Variable - A Memory Space That Can
Variable - A Memory Space That Can
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();
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