0% found this document useful (0 votes)
33 views5 pages

Variable - A Memory Space That Can

The document discusses object-oriented programming concepts like classes, objects, data members, member functions, constructors and destructors. It provides examples of defining classes for students with data members like registration number, name and marks. It also shows examples of using parameterized and non-parameterized constructors.

Uploaded by

brianmtira199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Variable - A Memory Space That Can

The document discusses object-oriented programming concepts like classes, objects, data members, member functions, constructors and destructors. It provides examples of defining classes for students with data members like registration number, name and marks. It also shows examples of using parameterized and non-parameterized constructors.

Uploaded by

brianmtira199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Variable - A memory space that can contain a value(int, float,double,char,

bool{inbuilt or primitive data types}), array, object (structure, class)


struct struct_name
{
data members;
};

Example:
#include<iostream>
using namespace std;
struct student
{
string regNum, name;
int marks;
};
main()
{
student s;
s.regNum="SCT213-c002-0023/2023";
s.name="Carol Nyanjui" ;
s.marks=78;
}
#include<iostream>
using namespace std;
class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
};
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks;
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s;
s.enter_student_details();
s.display_student_details();
}

#include<iostream>
using namespace std;
class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
}s1;
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s;
cout<<"Enter the details of the first student"<<endl;
cout<<"----------------------------------------"<<endl;
s.enter_student_details();
s.display_student_details();

cout<<"Enter the details of the second student"<<endl;


cout<<"------------------------------------------"<<endl;
s1.enter_student_details();
s1.display_student_details();
}

Format of an OOP program


#include<iostream>
using namespace std;
class class_name
{
access_specifier:
data members/instant variables;
function/methods members
}object_name;

main()
{
class_name object_name;
//the body of the main
}
where:
1. access_specifier:private,protected,public
2. class_name(user-defined data type) and object_name is any valid meaningful
identifier
3. data members/instant variables - any valid meaningful identifier that stores
data
4. function/methods members - subprograms used to manipulate data members:
can be member functions: operations/methods
constructors, destructors,inline and non-member:
friend functions
Lesson 10: Inline, Friend, Constructor and Destructor Functions
float length,width,area;
#include<iostream>
using namespace std;
main()
{
float length,width,area,peri;
area=length*width;
peri=2*(length+width);
cout<<"The length is: "<<length<<endl;
cout<<"The width is: "<<width<<endl;
cout<<"The area is: "<<area<<endl;
cout<<"The perimeter is: "<<peri<<endl;
}
Initlization of variables
syntax:
datatype variable_name=initial_value; // used to initialize variables that contain
only one value
#include<iostream>
using namespace std;
main()
{
float length=0.0,width=0.0,area=0.0,peri=0.0;
cout<<"Computing the area and perimeter using the initial values"<<endl;
cout<<"----------------------------------------------------------------"<<endl;
area=length*width;
peri=2*(length+width);
cout<<"The length is: "<<length<<endl;
cout<<"The width is: "<<width<<endl;
cout<<"The area is: "<<area<<endl;
cout<<"The perimeter is: "<<peri<<endl;

cout<<"Computing the area and perimeter using the our own values"<<endl;

cout<<"-------------------------------------------------------------------"<<endl;
cout<<"Enter the length of the rectangle: ";
cin>>length;
cout<<"Enter the width of the rectangle: ";
cin>>width;
area=length*width;
peri=2*(length+width);
cout<<"The length is: "<<length<<endl;
cout<<"The width is: "<<width<<endl;
cout<<"The area is: "<<area<<endl;
cout<<"The perimeter is: "<<peri<<endl;
}

CONSTRUCTORS
A constructor is a 'special ' member function whose task is to automatically
initialize the objects of its class. It is special because its name is the same as
the class name. The constructor is invoked whenever an object of its associated
class is created. It does not have a return value not even void. It is called a
constructor because it constructs the values of data members of the class.
Characteristics
It does not have a return value not even void
It has the same name as a class
It can have parameters (parameterized constructors) or no parameters(default or
parameterized constructors)
They should be declared in the public section
They should be invoked automatically when the objects are created
They cannot be inherited, though a derived class can call the base class
constructor

data member or member variables


#include<iostream>
using namespace std;
class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
student(); //default or non-parameterized constructor
student(string, string, int); ////parameterized constructor
~student();
}s1;
student::student() //default or non-parameterized constructor
{
regNum="SCT213-C002-0001/2024";
name="George Morara";
marks=40;
}
student::student(string r, string n, int m)//parameterized constructor
{
regNum=r;
name=n;
marks=m;
}
student::~student()
{
cout<<"Objects have been destroyed"<<endl;
}
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks;
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s("SCT213-c002-0023/2023","Carol Nyanjui",45);
cout<<"Initilization using the parameterized constructors "<<endl;
cout<<"-----------------------------------------------------"<<endl;
s.display_student_details();
cout<<"Initilization using the non-parameterized constructors "<<endl;
cout<<"---------------------------------------------------------"<<endl;
s1.display_student_details();

cout<<"Enter the details of the first student"<<endl;


cout<<"----------------------------------------"<<endl;
s.enter_student_details();
s.display_student_details();

cout<<"Enter the details of the second student"<<endl;


cout<<"------------------------------------------"<<endl;
s1.enter_student_details();
s1.display_student_details();
}

You might also like