C++
Class and Objects
§ Class is a blueprint of an object
For example;
When a building is made, firstly we prepared a map for that building, which show how the building is showing in future.
But the map is not a building.
OBJECT:
It is a big memory of the block which contains multiple variables.
Variable is a block of containing single data.
§ When you define a class you will make a new data type.
§ This new data type is called non-primitive data type
§ Class is a description of an object’s property set and set of operations.
The variable contains a single value but,
The object contains the multiple values means contains a full records
§ Which type of values object will contain, define in class.
§ In class, we also define total no of variables and their size.
§ Class have also functions which used the variables of objects.
§ Creating a class is similar to creating a structure in C language.
§ Class is a means to achieve encapsulation.
Encapsulation means making a single group of functions, properties to a related item.
§ Object is a run time entity
§ Object takes memory but, the class doesn’t take.
§ Class is only the description of objects.
§ Memory of object depends on the object’s variables in it.
Format of Class
§ Here the box is the new data type.
§ box b1 ;
Here the box is the data type, b1 is the object.
§ Nature of object b1 and how many memory it will consume all the details are defined in a class of that object.
§ box b1;
§ box b2;
§ box b3;
§ Here l, b, h of b1, b2 and b3 are different from each other.
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b,sum; //data members
public: // access specifiers
void getdata() //memeber functions
{
cout<<"Enter the First Number : ";
cin>>a;
cout<<"Enter the second number : ";
cin>>b;
}
void display()
{
sum=a+b;
cout<<"Result : "<<sum<<endl;
}
};
void main()
{
clrscr();
demo d,d1;
d.getdata();
d.display();
d1.getdata();
d1.display();
getch();
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b,c; //data members
public: // access specifiers
void input() //memeber functions
{
cout<<"Enter the First Number : ";
cin>>a;
cout<<"Enter the second number : ";
cin>>b;
}
void multiply()
{
c=a*b;
}
void show()
{
cout<<"Multiplication : "<<c;
}
};
void main()
{
clrscr();
demo d;
d.input();
d.multiply();
d.show();
getch();
}
#include<iostream.h>
#include<conio.h>
class volume
{
int l,b,h,v; //data members
public: // access specifiers
void input() //memeber functions
{
cout<<"Enter Length : ";
cin>>l;
cout<<"Enter breadth : ";
cin>>b;
cout<<"Enter height : ";
cin>>h;
}
void vol()
{
v=l*b*h;
}
void display()
{
cout<<"\nEntered Details :\n";
cout<<"length : "<<l<<endl;
cout<<"Breadth :"<<b<<endl;
cout<<"Height : "<<h<<endl;
cout<<"Volumn : "<<v<<endl;
}
};
void main()
{
clrscr();
volume v,v1;
v.input();
v.vol();
v.display();
v1.input();
v1.vol();
v1.display();
getch();
}
}
Example 1
#include<conio.h>
class demo
{
int a,b,sum; //data members
public: // access specifiers
void getdata() //memeber functions
{
cout<<"Enter the First Number : ";
cin>>a;
cout<<"Enter the second number : ";
cin>>b;
}
void display()
{
sum=a+b;
cout<<"Result : "<<sum<<endl;
}
};
void main()
{
clrscr();
demo d,d1;
d.getdata();
d.display();
d1.getdata();
d1.display();
getch();
Example 2
#include<conio.h>
class demo
{
int a,b,c; //data members
public: // access specifiers
void input() //memeber functions
{
cout<<"Enter the First Number : ";
cin>>a;
cout<<"Enter the second number : ";
cin>>b;
}
void multiply()
{
c=a*b;
}
void show()
{
cout<<"Multiplication : "<<c;
}
};
void main()
{
clrscr();
demo d;
d.input();
d.multiply();
d.show();
getch();
}
Example 3
#include<iostream.h>
#include<conio.h>
class volume
{
int l,b,h,v; //data members
public: // access specifiers
void input() //memeber functions
{
cout<<"Enter Length : ";
cin>>l;
cout<<"Enter breadth : ";
cin>>b;
cout<<"Enter height : ";
cin>>h;
}
void vol()
{
v=l*b*h;
}
void display()
{
cout<<"\nEntered Details :\n";
cout<<"length : "<<l<<endl;
cout<<"Breadth :"<<b<<endl;
cout<<"Height : "<<h<<endl;
cout<<"Volumn : "<<v<<endl;
}
};
void main()
{
clrscr();
volume v,v1;
v.input();
v.vol();
v.display();
v1.input();
v1.vol();
v1.display();
getch();
}
**You reached end of lecture**

Post a Comment
0 Comments