You are on page 1of 15

1.

PROGRAM TO ILLUSTRATE CLASS AND OBJECT


a) PROGRAM TO ADD TWO NUMBERS

#include<iostream>
using namespace std;
class sum
{
public:
int a,b,c;
void add()
{
cout<<"Enter the numbers:";
cin>>a>>b;
c=a+b;
cout<<"sum is : "<<c;
}
}obj;
int main()
{
obj.add();
return 0;
}
b) PROGRAM FOR LARGEST OF THREE NUMBERS

#include<iostream>
using namespace std;
class largestt
{
public:
int a,b,c;
void largest()
{
cout<<"Enter three number:";
cin>>a>>b>>c;
if(a>b && a>c)
cout<<"a is largest"<<a;
else if(b>a && b>c)
cout<<"b is largest"<<b;
else
cout<<"c is largest"<<c;
}
}obj;
int main()
{
obj.largest();
return 0;
}
C) PROGRAM FOR ADDITION OF TWO MATRIX

#include<iostream>
using namespace std;
class matrix
{
public:
int row,col,a[20][20],b[20][20],sum[20][20],i,j;
void input()
{
cout<<"enter number of rows:";
cin>>row;
cout<<"enter number of columns:";
cin>>col;
cout<<"enter the values for first matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
cin>>a[i][j];
}
cout<<"enter the values for second matrix:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
cin>>b[i][j];
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
sum[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
cout<<sum[i][j];
cout<<"\n";
}
}}obj;
int main()
{
obj.input();
return 0;
}
d) PROGRAM TO FIND AREA OF CIRCLE

#include<iostream>
using namespace std;
class circle
{
public:
int r;
float area;
void Area()
{
cout<<"Enter the radius";
cin>>r;
area=3.14*r*r;
cout<<area;
}
}obj;
int main()
{
obj.Area();
return 0;
}
2. PROGRAM TO ILLUSTRATE INLINE MEMBER
FUNCTION

#include<iostream>
using namespace std;
class operation{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void diff();
void mult();
void divi();
};
inline void operation::get(){
cout<<"Enter two numbers:";
cin>>a>>b;
}
inline void operation::sum()
{
add=a+b;
cout<<"\n sum is:"<<add;
}
inline void operation::diff()
{
sub=a-b;
cout<<"\n Difference is:"<<sub;
}
inline void operation::mult()
{
mul=a*b;
cout<<"\n product is:"<<mul;
}
inline void operation::divi()
{
div=a/b;
cout<<"\n Divided value is"<<div;
}
int main(){
cout<<"Program using inline function\n";
operation s;
s.get();
s.sum();
s.diff();
s.mult();
s.divi();
return 0;
3.PROGRAM TO ILLUSTRATE STATIC DATA AND MEMBER
FUNCTION

#include<iostream>
using namespace std;
class statt
{
int code;
static int count;
public:
statt()
{
code=++count;
}
void showcode()
{
cout<<"\n\tObject number is:"<<code;
}
static void showcount()
{
cout<<"\n\tCount objects:"<<cout;
}
};
int statt::count;
int main(){
statt obj1,obj2;
obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
return 0;}
4. PROGRAM TO ILLUSTARTE CONSTRUCTOR
a) PROGRAM TO IMPLEMENT LINEAR SEARCH USING
DEFAULT CONSTUCTOR

#include<iostream>
using namespace std;
class linear
{
public:
int num,i,key,found;
int array[];
linear()
{
cout<<"Enter the size of array:";
cin>>num;
cout<<"Enter the array elements:";
for(i=0;i<num;i++)
{
cin>>array[i];
}
cout<<"Enter the search element:";
cin>>key;
for(i=0;i<num;i++)
{
if(array[i]==key)
{
found=1;
break;
}
}
if(found==1)
{
cout<<"The element is found at position:"<<i+1;
}
else
{
cout<<"Element not found";
}
}};
int main()
{
linear l1;
return 0;
}
b) PROGRAM TO FIND THE AREA OF THE RECTANGLE
USING PARAMETRISED CONSTRUCTOR

#include<iostream>
using namespace std;
class rectangle
{
public:
float area;
void Area(int l,int b)
{
area=l*b;
cout<<”area of the rectangle:”<<area;
}
}obj;
int main()
{
int a,b;
cout<<"Enter the length and breadth of the rectangle:";
cin>>a>>b;
obj.Area(a,b);
return 0;
}
5. PROGRAM TO ILLUSTRATE FRIEND FUNCTION

#include<iostream>
using namespace std;
class matrix{
public:
int row,col,i,j,a[20][20],b[20][20],sub[20][20];
void input(){
cout<<"Enter the rows";
cin>>row;
cout<<"Enter the number of col:";
cin>>col;
cout<<"Enter the values of first matrix:\n";
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>a[i][j];
cout<<"Enter the values of second matrix:\n";
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>b[i][j];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
sub[i][j]=a[i][j]-b[i][j];
}
friend void display(matrix c);
};

void display(matrix c){


cout<<"difference of matrix is:";
cout<<"\n";
for(c.i=0;c.i<c.row;c.i++){
for(c.j=0;c.j<c.col;c.j++){
c.sub[c.i][c.j]=c.a[c.i][c.j]-c.b[c.i][c.j];
cout<<"\t"<<c.sub[c.i][c.j]<<"";
}
cout<<"\n";
}
}

int main(){
matrix m;
m.input();
display(m);
return 0;
}
6. PROGRAM TO ILLUSTARTE OPERATOR OVERLOADING
a) PROGRAM FOR UNARY OPERATOR OVERLOADING

#include<iostream>
using namespace std;
class check
{
int i;
public:
check()
{
i=1;
}
void operator++()
{
i=i+10;
}
void display()
{
cout<<"i="<<i;
}
}obj;
int main()
{

++obj;
obj.display();
return 0;
}
b) PROGRAM FOR BINARY OPERATOR OVERLOADING

#include<iostream>
using namespace std;
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value of complex numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
int main()
{
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result=obj1+obj2;
result=obj1-obj2;
cout<<"input Values:\n";obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
return 0;
}
7. PROGRAM TO ILLUSTRATE FUNCTION OVERLOADING

#include<iostream>
using namespace std;

class fn{
public:
void area(int);
void area(int, int);
void area(float, int, int);
};

void fn::area(int a){cout<<"Area of circle:"<<3.14*a*a;}


void fn::area(int a,int b){cout<<"Area of rectangle:"<<a*b;}
void fn::area(float t,int a,int b){cout<<"Area of triangle:"<<t*a*b;}

int main(){
int ch;
int a,b,r;
fn obj;
cout<<"\n\tFunction overloading";
cout<<"\n1.Area of circle\n2.Area of rectange\n3.Area of triangle\n4.Exit:";
cout<<"Enter your choice:";
cin>>ch;
switch(ch){
case 1:
cout<<"Enter radius of circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter sides of the rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
cout<<"\n";
exit(0);
}
return 0;
}
8. PROGRAM TO ILLUSTRATE INHERITANCE

#include<iostream>
using namespace std;
class example
{
protected:
int x;
public:
void getdata()
{
cout<<"Enter the value of x:";
cin>>x;
}
};
class ex:public example
{
private:
int y;
public:
void readdata()
{
cout<<"Enter the value of y:";
cin>>y;
}
void product()
{
cout<<"product="<<x*y;
}
};
int main()
{
ex a;
a.getdata();
a.readdata();
a.product();
return 0;
}
9.PROGRAM TO ILLUSTRATE POINTER TO OBJECTS

#include<iostream>

Using namespace std;

class myclass

int i;

public:

void read(int j)

i=j;

int getint()

return i;

};

int main()

myclass ob,*objectpointer;

objectpointer=&ob;

objectpointer->read(10);

cout<<objectpointer->getint();

return 0;

}
10.PROGRAM TO ILLUSTRATE VIRTUAL FUNCTION

#include<iostream>
using namespace std;
class First
{
public:
virtual void calculate()
{
cout<<"Area Perimeter of a rectangle";
}
};
class Second:public First
{
public:
int width,height,area,perimeter;
void calculate()
{
cout<<"Enter width of rectangle:";
cin>>width;
cout<<"Enter height of rectangle:";
cin>>height;
area=height*width;
cout<<"Area of Rectangle:"<<area;
perimeter=2*(height+width);
cout<<"\nPerimeter of rectangle:"<<perimeter;
}
};
int main()
{
First*f;
Second s;
f=&s;
f->calculate();
return 0;
}

You might also like