Oops Assignment
Oops Assignment
.POLYTECHNIC
Object Oriented Programming
Submitted By :
Name - Aditya Chik Baraik
Department - Computer Science and Technology
Registration No - D192021775
Roll - DSLGCSTS4 No- 10022089
Page No : 1
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter the Two Numbers =" << endl;
cin >> a >> b;
cout << "Sum =" << (a + b) << endl;
cout << "Average =" << ((a + b) / 2) << endl;
}
Output :
22
12
Sum =34
Average =17
Page No : 2
#include<iomanip>
using namespace std;
int main()
{
int m=2597;
int n=14;
int p=175;
cout<<setw(5)<<m<<endl;
cout<<setw(5)<<n<<endl;
cout<<setw(5)<<p<<endl;
return 0;
}
Output :
2597
14
175
Page No : 3
#include<iostream>
using namespace std;
class smallobj
{
private:
int somedata;
public:
void setdata(int d)
{
somedata=d;
}
void showdata()
{
cout<<"Data is "<<somedata<<"\n";
}
};
int main()
{
Page No : 4
smallobj s1,s2;
s1.setdata(1066);
s2.setdata(1776);
s1.showdata();
s2.showdata();
return 0;
}
Output :
Data is 1066
Data is 1776
#include <iostream>
using namespace std;
class smallobj
{
private:
int somedata;
public:
void setdata(int d)
{
somedata = d;
}
void showdata()
{
cout << "Data is " << somedata << "\n";
}
};
int main()
{
smallobj s1, s2;
Page No : 6
s1.setdata(2019);
s2.setdata(2003);
s1.showdata();
s2.showdata();
return 0;
}
Output :
Data is 2019
Data is 2003
#include<iostream>
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number =a;
count++;
}
void getcount(void)
{
cout<<"Count:";
cout<<count<<"\n";
}
};
int item::count;
int main()
Page No : 8
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
a.getdata(200);
a.getdata(300);
cout<<"After reading data"<<"\n";
a.getcount();
a.getcount();
a.getcount();
return 0;
}
Output :
Count:0
Count:0
Page No : 9
Count:0
Count:3
Count:3
Count:3
#include<iostream>
using namespace std;
class item
{
int code;
static int count;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
}
static void showcount(void)
{
cout<<count<<"\n";
}
Page No : 11
};
int item::count;
int main()
{
item a,b;
a.setcode();
b.setcode();
item::showcount();
item c;
c.setcode();
item::showcount();
a.showcode();
b.showcode();
c.showcode();
return 0;
}
Output :
Count = 2
Page No : 12
Count = 3
object number = 1
object number = 2
object number = 3
Page No : 13
#include<iostream>
using namespace std;
class stack
{
private:
enum{MAX=10};
int st[MAX];
int top;
public:
stack()
{
top=0;
}
void push(int var)
{
st[++top]=var;
}
int pop()
{
Page No : 14
return st[top--];
}
};
int main()
{
stack s1;
s1.push(11);
s1.push(22);
cout<<"1 = "<<s1.pop()<<"\n";
cout<<"2 = "<<s1.pop()<<"\n";
s1.push(33);
s1.push(44);
s1.push(55);
s1.push(66);
cout<<"3 = "<<s1.pop()<<"\n";
cout<<"4 = "<<s1.pop()<<"\n";
cout<<"5 = "<<s1.pop()<<"\n";
cout<<"6 = "<<s1.pop()<<"\n";
return 0;
Page No : 15
}
Output :
1 = 22
2 = 11
3 = 66
4 = 55
5 = 44
6 = 33
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inch;
public:
Distance();
void getDist();
void showDist();
Distance addDist(Distance d2);
Distance subDist(Distance d2);
};
Distance::Distance()
{
feet = 0;
inch = 0;
}
void Distance::getDist()
Page No : 17
{
cout << "\nEnter feet : ";
cin >> feet;
cout << "\nEnter inches : ";
cin >> inch;
inch = (inch >= 12) ? 12 : inch;
}
void Distance ::showDist()
{
cout << endl<<endl
<< "\tFeets : " << feet;
cout << endl
<< "\tInches : " << inch;
}
Distance Distance ::addDist(Distance d2)
{
Distance temp;
temp.feet = feet + d2.feet;
temp.inch = inch + d2.inch;
if (temp.inch >= 12)
{
Page No : 18
temp.feet++;
temp.inch -= 12;
}
return temp;
}
Distance Distance::subDist(Distance d2)
{
Distance temp;
temp.feet = feet - d2.feet;
temp.inch = inch - d2.inch;
}
int main()
{
Distance d1;
Distance d2;
Distance d3;
Distance d4;
cout << "\nEnter Distance1 : " << endl;
d1.getDist();
cout << "\nEnter Distance2 : " << endl;
d2.getDist();
Page No : 19
d3 = d1.addDist(d2);
d4 = d1.subDist(d2);
cout << endl
<< "Distance1 : ";
d1.showDist();
cout << endl
<< "Distance2 : ";
d2.showDist();
cout << endl
<< "Distance3 : ";
d3.showDist();
cout << endl
<< "Distance4 : ";
d4.showDist();
cout << endl;
return 0;
}
Output :
Enter Distance1 :
Enter feet : 5
Page No : 20
Enter inches : 1
Enter Distance2 :
Enter feet : 3
Enter inches : 4
Distance1 :
Feets : 5
Inches : 1
Distance2 :
Feets : 3
Inches : 4
Distance3 :
Feets : 8
Inches : 5
Distance4 :
Feets : 2
Inches : -3
#include<iostream>
using namespace std;
class employee
{
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
void employee::getdata()
{
cout<<"Enter Name = ";
cin>>name;
cout<<"Enter Age = ";
cin>>age;
}
void employee::putdata()
{
Page No : 22
cout<<"\tName :"<<name<<"\n";
cout<<"\tAge :"<<age<<"\n";
}
const int size=3;
int main()
{
int i;
employee manager[size];
for(i=0;i<size;i++)
{
cout<<"\nDetails of manager : "<<i+1<<"\n";
manager[i].getdata();
}
cout<<"\n";
for(i=0;i<size;i++)
{
cout<<"Manager = "<<i+1<<"\n";
manager[i].putdata();
}
return 0;
}
Page No : 23
Output :
Details of manager : 1
Enter Age = 18
Details of manager : 2
Enter Age = 22
Details of manager : 3
Enter Age = 24
#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
void input(float real,float imag)
{
x=real;y=imag;
}
friend complex sum(complex,complex);
void show (complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return (c3);
Page No : 25
}
void complex::show(complex c)
{
cout<<c.x<<"+i"<<c.y<<"\n";
}
int main()
{
complex A,B,C;
A.input (3.1,2.65);
B.input(2.2,3.75);
C=sum(A,B);
cout<<"A=";
A.show(A);
cout<<"B=";
B.show(B);
cout<<"C=";
C.show(C);
return 0;
}
Output :
Page No : 26
A = 3.1+i2.65
B = 2.2+i3.75
C = 5.3+i6.4
using namespace std;
class CRectangle
Page No : 27
{
int width,height;
public:
CRectangle(int,int);
int area()
{
return(width*height);
}
};
CRectangle::CRectangle(int a,int b)
{
width=a;
height=b;
}
int main()
{
CRectangle rect(3,4);
CRectangle rectb(5,6);
cout<<"Rectangle A area : "<<rect.area()<<"\n";
cout<<"Rectangle B area : "<<rectb.area()<<"\n";
return 0;
Page No : 28
}
Output :
Rectangle A area : 12
Rectangle B area : 30
#include<iostream>
using namespace std;
Page No : 29
class integer
{
int m,n;
public:
integer(int,int);
void display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
integer::integer(int x,int y)
{
m=x;n=y;
}
int main()
{
integer int1(0,100);
integer int2=integer(25,75);
cout<<"\nObject by Implicit constructor call:"<<"\n";
int1.display();
Page No : 30
cout<<"\nObject by Explicit constructor call:"<<"\n";
int2.display();
}
Output :
Object by Implicit constructor call:
m=0
n=100
m=25
n=75
#include<iostream>
using namespace std;
Page No : 31
class integer
{
int m,n;
public:
integer(int,int);
void display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
integer::integer(int x,int y)
{
m=x;n=y;
}
int main()
{
integer int1(120,200);
integer int2=integer(55,15);
cout<<"\nObject by Implicit constructor call : "<<"\n";
Page No : 32
int1.display();
cout<<"\nObject by Explicit constructor call : "<<"\n";
int2.display();
}
Output :
Object by Implicit constructor call :
m=120
n=200
m=55
n=15
Page No : 33
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
class figure
{
private:
float radius, side1, side2, side3;
char shape[10];
public:
figure(float r)
{
radius = r;
strcpy(shape, "Circle");
}
figure(float s1, float s2)
{
side1 = s1;
side2 = s2;
side3 = radius = 0.0;
Page No : 34
strcpy(shape, "Rectangle");
}
figure(float s1, float s2, float s3)
{
side1 = s1;
side2 = s2;
side3 = s3;
radius = 0.0;
strcpy(shape, "Triangle");
}
void area()
{
float ar, s;
if (radius == 0.0)
{
if (side3 == 0.0)
{
ar = side1 * side2;
}
else
{
Page No : 35
s = (side1 + side2 + side3) / 2;
ar = pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
}
else
ar = 3.14 * radius * radius;
cout << "\n\nArea of the " << shape << " is : " << ar << " sq.units\n"
;
}
};
int main()
{
figure circle(10.0);
figure rectangle(15.0, 20.6);
figure triangle(3.0, 4.0, 5.0);
circle.area();
rectangle.area();
triangle.area();
return 0;
}
Output :
Page No : 36
#include<iostream>
Page No : 37
using namespace std;
class complex
{
float x,y;
public:
complex(){};
complex (float a)
{
x=y=a;
}
complex (float real, float imag)
{
x=real; y=imag;
}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum (complex c1,complex c2)
{
complex c3;
Page No : 38
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void show(complex c)
{
cout<<c.x<<"+i"<<c.y<<"\n";
}
int main()
{
complex A(2.7,3.5);
complex B(1.6);
complex C;
C=sum(A,B);
cout<<"A = "; show(A);
cout<<"B = "; show(B);
cout<<"C = "; show(C);
return 0;
}
Output :
Page No : 39
A = 2.7+i3.5
B = 1.6+i1.6
C = 4.3+i5.1
16)C++ program to illustrate the dynamic initialisation as
memory is allocated to the object .
#include <iostream>
using namespace std;
Page No : 40
class bank {
int principal;
int years;
float interest;
float returnvalue;
public:
bank() {}
bank(int p, int y, float i)
{
principal = p;
years = y;
interest = i/100;
returnvalue = principal;
cout << "\nDeposited amount (float):";
for (int i = 0; i < years; i++) {
returnvalue = returnvalue
* (1 + interest);
}
}
Page No : 41
bank(int p, int y, int i)
{
principal = p;
years = y;
interest = float(i)/100;
returnvalue = principal;
cout << "\nDeposited amount"
<< " (integer):";
for (int i = 0; i < years; i++) {
returnvalue = returnvalue
* (1 + interest);
}
}
void display(void)
{
cout << returnvalue
<< endl;
}
};
int main()
{
Page No : 42
int p = 200;
int y = 2;
int I = 5;
float i = 2.25;
bank b1(p, y, i);
b1.display();
bank b2(p, y, I);
b2.display();
return 0;
}
Output :
Deposited amount (float):209.101
#include <iostream>
using namespace std;
Page No : 43
class code
{
int id;
public:
code() {}
code(int a)
{
id = a;
}
code(code &x)
{
id = x.id;
}
void display()
{
cout << id;
}
};
int main()
{
Page No : 44
code A(100);
code B(A);
code C = A;
code D;
D = A;
cout << "\n Id of A : ";
A.display();
cout << "\n Id of B : ";
B.display();
cout << "\n Id of C : ";
C.display();
cout << "\n Id of D : ";
D.display();
}
Output :
Id of A : 100
Id of B : 100
Id of C : 100
Id of D : 100
Page No : 45
#include <iostream>
#include <string.h>
using namespace std;
Page No : 46
class string
{
char *name;
int length;
public:
string()
{
length = 0;
name = new char[length + 1]; /* one extra for \0 */
}
string(char *s) //constructor 2
{
length = strlen(s);
name = new char[length + 1];
strcpy(name, s);
}
void display(void)
{
cout << name << endl;
Page No : 47
}
void join(string &a.string &b)
{
length = a.length + b.length;
delete name;
strcpy(name, a.name);
strcat(name, b.name);
}
};
int main()
{
char *first = “Joseph” ;
string name1(first), name2(“louis”), naine3( “LaGrange”), sl, s2;
sl.join(name1, name2);
s2.join(s1, name3);
namel.display();
name2.display();
name3.display();
s1.display();
s2.display();
Page No : 48
}
Output :
Joseph
Louis
language
Joseph Louis
#include<iostream>
using namespace std;
Page No : 49
class matrix
{
int **p;
int d1,d2;
public:
matrix(int x, int y);
void get_element(int i,int j,int value)
{
p[i][j]=value;
}
int & put_element(int i,int j)
{
return p[i][j];
}
};
matrix::matrix(int x,int y)
{
d1=x;
d2=y;
p=new int *[d1];
Page No : 50
int i;
for(i=0;i<d1;i++)
p[i]=new int [d2];
}
int main()
{
int m,n;
cout<<"Enter size Row and Column of the Matrix : ";
cin>>m>>n;
matrix A(m,n);
cout<<"Enter Matrix elements (row wise) = \n";
int i,j,value;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>value;
A.get_element(i,j,value);
}
}
cout<<"\n The 2D array is : \n";
Page No : 51
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<A.put_element(i,j)<<"\n";
}
cout<<"\n";
}
}
Output :
123456
The 2D array is :
1
2
3
Page No : 52
4
5
6
using namespace std;
int divide(int a,int b=2)
{
Page No : 53
int r;
r=a/b;
return(r);
}
int main()
{
cout<<divide(12);
cout<<"\n";
cout<<divide(20,4);
return 0;
}
Output :
6
5
#include<iostream>
using namespace std;
Page No : 54
void swap(int &,int &);
int main()
{
int num1,num2;
cout<<"Enter two values :\n";
cout<<"num1 = ";
cin>>num1;
cout<<"\n";
cout<<"num2 = ";
cin>>num2;
cout<<"\n Before calling swap function :\n";
cout<<"num1 = "<<num1<<"\n";
cout<<"num2 = "<<num2;
swap(num1,num2);
cout<<"\n After calling swap function :\n";
cout<<"num1 = "<<num1<<"\n";
cout<<"num2 = "<<num2;
return 0;
}
void swap(int &a, int &b)
{
Page No : 55
cout<<"\n Before swapping :\n";
cout<<"a = "<<a<<"\n";
cout<<"b = "<<b;
int temp=a;
a=b;
b=temp;
cout<<"\n After swapping :\n";
cout<<"a = "<<a<<"\n";
cout<<"b = "<<b;
}
Output :
Enter two values :
num1 = 12
num2 = 15
num1 = 12
num2 = 15
Before swapping :
Page No : 56
a = 12
b = 15
After swapping :
a = 15
b = 12
num1 = 15
num2 = 12
#include<iostream>
using namespace std;
inline int min (int x,int y){return(x<y?x:y);}
Page No : 57
int main()
{
int num1,num2;
cout<<"Enter two numbers:\n";
cin>>num1>>num2;
cout<<"min(num1,num2):"<<min(num1,num2)<<"\n";
return 0;
}
Output :
32 10
min(num1,num2) : 10
Function Overloading.
#include<iostream>
using namespace std;
Page No : 58
int operate(int a, int b)
{
return (a*b);
}
float operate(float a, float b)
{
return (a/b);
}
int main()
{
int x=5,y=2;
float m=5.0,n=2.0;
cout<<operate(x,y);
cout<<"\n";
cout<<operate(m,n);
cout<<"\n";
return 0;
}
Output :
10
Page No : 59
2.5
#include<iostream>
using namespace std;
class sample
{
int a;
Page No : 60
int b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean (sample s)
{
return float (s.a+s.b)/2.0;
}
int main()
{
sample x;
x.setvalue();
cout<<"Mean value : "<<mean(x)<<"\n";
return 0;
}
Page No : 61
Output :
#include<iostream>
using namespace std;
class CRectangle
{
int width,height;
public:
Page No : 62
void set_values(int,int);
int area()
{
return(width*height);
}
friend CRectangle duplicate(CRectangle);
};
void CRectangle::set_values(int a,int b)
{
width=a;
height=b;
}
CRectangle duplicate(CRectangle rectparam)
{
CRectangle rectres;
rectres.width=rectparam.width*2;
rectres.height=rectparam.height*2;
return(rectres);
}
int main()
{
Page No : 63
CRectangle rect,rectb;
rect.set_values(2,3);
rectb=duplicate(rect);
cout<<rectb.area();
return 0;
}
Output :
24
#include <iostream>
using namespace std;
class counter
{
private:
unsigned int count;
Page No : 64
public:
counter() : count(0) {}
unsigned int get_count(){
return count;
}
void operator++()
{
++count;
}
};
int main()
{
counter c1, c2;
cout << "\n c1 = " << c1.get_count();
cout << "\n c2 = " << c2.get_count();
++c1;
++c2;
++c2;
cout << "\n\nc1 = " << c1.get_count();
cout << "\nc2 = " << c2.get_count();
Page No : 65
return 0;
}
Output :
c1 = 0
c2 = 0
c1 = 1
c2 = 2
#include<iostream>
using namespace std;
class unary
{
int x;
int y;
int z;
Page No : 66
public:
void getdata(int a,int b, int c);
void display();
void operator -();
};
void unary::getdata(int a, int b,int c)
{
x=a;y=b;z=c;
}
void unary::display()
{
cout<<x<<" "<<y<<" "<<z<<"\n";
}
void unary::operator -()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
Page No : 67
unary obj1;
obj1.getdata(10,20,30);
cout<<"\nObj1 = ";
obj1.display();
cout<<"\nObj1 = ";
-obj1;
obj1.display();
return 0;
}
Output :
Obj1 = 10 20 30
Page No : 68
#include<iostream>
using namespace std;
class unary
{
int x;
int y;
int z;
Page No : 69
public:
void getdata(int a,int b, int c);
void display();
friend void operator -(unary &u);
};
void unary::getdata(int a, int b,int c)
{
x=a;y=b;z=c;
}
void unary::display()
{
cout<<x<<" "<<y<<" "<<z<<"\n";
}
void operator -(unary &u)
{
u.x=-u.x;
u.y=-u.y;
u.z=-u.z;
}
int main()
{
Page No : 70
unary obj1;
obj1.getdata(20,30,50);
cout<<"\nobj1 = ";
obj1.display();
cout<<"\nobj1 = ";
-obj1;
obj1.display();
return 0;
}
Output :
Obj1 = 20 30 50
Obj1 = -20 -30 -50
Page No : 71
#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
Page No : 72
complex(){ };
complex(float real,float imag){x=real;y=imag;}
complex operator +(complex);
void display();
};
complex complex::operator +(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return (temp);
}
void complex::display()
{
cout<<x<<"+i"<<y<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
Page No : 73
c3=c1+c2;
cout<<"c1 = ";c1.display();
cout<<"c2 = ";c2.display();
cout<<"c3 = ";c3.display();
return 0;
}
Output :
c1 = 2.5+i3.5
c2 = 1.6+i2.7
c3 = 4.1+i6.2
#include <iostream>
using namespace std;
class invent1
{
int code;
int items;
Page No : 74
float price;
public:
invent1(){}
invent1(int a, int b, int c)
{
code = a;
items = b;
price = c;
}
void display()
{
cout << "\nCode : " << code;
cout << "\nItems : " << items;
cout << "\nPrice : " << price;
}
int getcode()
{
return code;
}
int getitem()
Page No : 75
{
return items;
}
int getprice()
{
return price;
}
};
class invent2
{
int code;
float value;
public:
invent2()
{
code = 0;
value = 0;
}
invent2(int x, float y)
{
code = x;
Page No : 76
value = y;
}
void display()
{
cout << "Code : " << code << endl;
cout << "Value : " << value << endl;
}
invent2(invent1 p)
{
code = p.getcode();
value = p.getitem() * p.getprice();
}
};
int main()
{
invent1 s1(100, 5, 140);
invent2 d1;
cout << "\nProduct details - Invent1 type";
s1.display();
cout << "\n\n\nProduct details - Invent2 type\n";
Page No : 77
d1.display();
return 0;
}
Output :
Product details - Invent1 type
Code : 100
Items : 5
Price : 140
Code : 100
Value : 700
#include <iostream>
using namespace std;
class invent1
{
int code;
int items;
Page No : 78
float price;
public:
invent1(){}
invent1(int a, int b, int c)
{
code = a;
items = b;
price = c;
}
void display()
{
cout << "\nCode : " << code;
cout << "\nItems : " << items;
cout << "\nPrice : " << price;
}
int getcode() { return code; }
int getitem() { return items; }
int getprice() { return price; }
};
Page No : 79
class invent2
{
int code;
float value;
public:
invent2()
{
code = 0;
value = 0;
}
invent2(int x, float y)
{
code = x;
value = y;
}
void display()
{
cout << "Code : " << code << endl;
cout << "Value : " << value << endl;
}
Page No : 80
invent2(invent1 p)
{
code = p.getcode();
value = p.getitem() * p.getprice();
}
};
int main()
{
invent1 s1(200, 15, 240);
invent2 d1;
d1 = s1;
cout << "\nProduct details - Invent1 type";
s1.display();
cout << "\n\n\nProduct details - Invent2 type\n";
d1.display();
return 0;
}
Output :
Page No : 81
Code : 200
Items : 15
Price : 240
Code : 200
Value : 3600
using namespace std;
class B //Base class
{
public:
int b;
void get_ab();
Page No : 82
int get_a(void);
void show_a();
};
class D : public B //Derived Class
{
int c;
public:
void mul();
void display();
};
void B::get_ab()
{
a = 5;
b = 10;
}
int B::get_a()
{
return (a);
}
void B::show_a()
{
Page No : 83
cout << "a = " << a << "\n";
}
void D::mul()
{
c = b * get_a();
}
void D::display()
{
cout << "a = " << get_a() << "\n";
cout << "b = " << b << "\n";
cout << "c = " << c << "\n";
}
int main()
{
D dobj;
dobj.get_ab();
dobj.mul();
dobj.show_a();
dobj.display();
dobj.b = 20;
dobj.mul();
Page No : 84
dobj.display();
}
Output :
a=5
a=5
b = 10
c = 50
a=5
b = 20
c = 100
#include <iostream>
using namespace std;
class B
{
public:
Page No : 85
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D : private B
{
int c;
public:
void mul(void);
void display(void);
};
void B::get_ab(void)
{
a = 5;
b = 10;
}
int B::get_a()
{
return (a);
Page No : 86
}
void B::show_a()
{
cout << "a= " << a << "\n";
}
void D::mul()
{
get_ab();
c = b * get_a();
}
void D::display()
{
show_a();
cout << "b= " << b << "\n";
cout << "c= " << c << "\n";
}
int main()
{
D dobj;
dobj.mul();
dobj.display();
Page No : 87
dobj.mul();
dobj.display();
return 0;
}
Output :
a= 5
b= 10
c= 50
a= 5
b= 10
c= 50
#include<iostream>
using namespace std;
class stud
{
protected:
int roll;
public:
Page No : 88
void get_no(int);
void put_no(void);
};
void stud::get_no(int a)
{
roll=a;
}
void stud::put_no()
{
cout<<"Roll No. : "<<roll<<"\n";
}
class test:public stud
{
protected:
float sub1;
float sub2;
public:
void get_marks(float,float);
void put_marks();
};
void test::get_marks(float x,float y)
Page No : 89
{
sub1=x;
sub2=y;
}
void test::put_marks()
{
cout<<"Marks in Subject 1 : "<<sub1<<"\n";
cout<<"Marks in Subject 2 : "<<sub2<<"\n";
}
class result:public test
{
float total;
public:
void display();
};
void result::display()
{
total=sub1+sub2;
put_no();
put_marks();
cout<<"Total = "<<total<<"\n";
Page No : 90
}
int main()
{
result s1;
s1.get_no(1);
s1.get_marks(80.0,70.0);
s1.display();
}
Output :
Roll No. : 1
Marks in Subject 1 : 80
Marks in Subject 2 : 70
Total = 150
Page No : 91
#include <iostream>
using namespace std;
class M
{
protected:
int m;
public:
Page No : 92
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P : public M, public N
{
public:
void display(void);
};
void M::get_m(int x)
{
m = x;
}
void N::get_n(int y)
{
Page No : 93
n = y;
}
void P::display()
{
cout << "m = " << m << "\n";
cout << "n = " << n << "\n";
cout << "m*n = " << m * n << "\n";
}
main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
}
Output :
m = 10
n = 20
m*n = 200
Page No : 94
#include <iostream>
using namespace std;
class student
{
protected:
int roll;
Page No : 95
public:
void get_roll(int a)
{
roll = a;
}
void put_roll(void)
{
cout << "Roll : " << roll << endl;
}
};
class test : virtual public student
{
protected:
float sub1, sub2;
public:
void get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
Page No : 96
{
cout << "Marks obtained : " << endl;
cout << "Sub1 : " << sub1 << endl;
cout << "Sub2 : " << sub2 << endl;
}
};
class sports : virtual public student
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score(void)
{
cout << "Sports score : " << score << endl;
}
};
Page No : 97
class result : public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total = sub1 + sub2 + score;
put_roll();
put_marks();
put_score();
cout << "Total score: " << total << "\n";
}
main()
{
result stud_res;
stud_res.get_roll(1);
stud_res.get_marks(30.5, 25.5);
stud_res.get_score(10.5);
Page No : 98
stud_res.display();
}
Output :
Roll : 1
Marks obtained :
Sub1 : 30.5
Sub2 : 25.5
#include <iostream>
using namespace std;
class A
{
int x, y;
public:
void show(int a)
{
Page No : 99
x = a;
cout << "I am 1st and x = " << x << "\n";
}
void show(int a, int b)
{
x = a;
y = b;
cout << "\nI am 2nd and x = " << x << " & y = " << y << "\n";
}
};
main()
{
A a1;
a1.show(10);
a1.show(20, 30);
}
Output :
I am 1st and x = 10
#include <iostream>
using namespace std;
class A
{
int x;
public:
void show(int a)
{
Page No : 101
x = a;
cout << "I am A and x = " << x;
}
};
class B : public A
{
int y;
public:
void show(int b)
{
y = b;
cout << "I am B and y = " << y;
}
};
main()
{
B b1;
b1.show(10);
}
Page No : 102
Output :
I am B and y = 10
#include <iostream>
using namespace std;
class item
{
int code;
float price;
public:
void getdata(int a, float b)
{
code = a;
Page No : 103
price = b;
}
void show()
{
cout << "CODE: " << code << endl;
cout << "PRICE: " << price << endl;
}
};
const int size = 2;
main()
{
item *p = new item[size];
item *d = p;
int x, i;
float y;
for (i = 0; i < size; i++)
{
cout << "Enter CODE and PRICE for item " << i + 1 << endl;
cin >> x >> y;
p->getdata(x, y);
p++;
Page No : 104
}
for (i = 0; i < size; i++)
{
cout << "ITEM: " << i + 1 << endl;
d->show();
d++;
}
}
Output :
12 1200
23 500
ITEM: 1
CODE: 12
PRICE: 1200
ITEM: 2
Page No : 105
CODE: 23
PRICE: 500
#include <iostream>
#include <string.h>
using namespace std;
class person
{
char name[20];
float age;
public:
person(char *s, float a)
{
Page No : 106
strcpy(name, s);
age = a;
}
person &person::greater(person &x)
{
if (x.age >= age)
return x; //argument object
else
return *this; //invoking object
}
void display(void)
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
}
};
void main()
{
person p1("Sachin", 37.50);
person p2("Sourav", 29.0);
person p3("Rahul", 40.25);
Page No : 107
person p('\0', 0);
p = p1.greater(p3);
cout << "Elder person is : \n";
p.display();
p = p1.greater(p2);
cout << "Elder person is : \n";
p.display();
}
Output :
Elder person is :
Name : Rahul
Age : 40.25
Elder person is :
Name : Sachin
Age : 37.5
Page No : 108
#include <iostream>
using namespace std;
class base
{
public:
int b;
void show()
{
cout << "b = " << b << "\n";
}
};
class derived : public base
{
Page No : 109
public:
int d;
void show()
{
cout << "b = " << b << "\n";
cout << "d = " << d << "\n";
}
};
main()
{
base *bptr;
base bobj;
bptr = &bobj;
bptr->b = 100;
cout << "bptr points to base object \n";
bptr->show();
derived dobj;
bptr = &dobj;
bptr->b = 200;
cout << "bptr now points to derived object \n";
bptr->show();
Page No : 110
derived *dptr;
dptr = &dobj;
dptr->d = 300;
cout << "dptr is derived type pointer \n";
dptr->show();
cout << "using ((derived *)bptr)\n";
((derived *)bptr)->d = 400;
((derived *)bptr)->show();
}
Output :
b = 100
b = 200
b = 200
d = 300
b = 200
Page No : 111
d = 400
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a;
height = b;
}
virtual int area()
Page No : 112
{
return (0);
}
};
class CRectangle : public CPolygon
{
public:
int area()
{
return (width * height);
}
};
class CTriangle : public CPolygon
{
public:
int area()
{
return (width * height / 2);
}
};
main()
Page No : 113
{
CRectangle rect;
CTriangle trgl;
CPolygon poly;
CPolygon *ppoly1 = ▭
CPolygon *ppoly2 = &trgl;
CPolygon *ppoly3 = &poly;
ppoly1->set_values(4, 5);
ppoly2->set_values(4, 5);
ppoly3->set_values(4, 5);
cout << "Area of Rectangle: " << ppoly1->area() << " sq. unit" << end
l;
cout << "Area of Triangle: " << ppoly2->area() << " sq. unit" << endl;
cout << "Area of Polygon: " << ppoly3->area() << " sq. unit" << endl;
}
Output :
#include <iostream>
using namespace std;
class CPolygon
{
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a;
height = b;
}
virtual int area() = 0;
};
Page No : 115
class CRectangle : public CPolygon
{
public:
int area()
{
return (width * height);
}
};
class CTriangle : public CPolygon
{
public:
int area()
{
return (width * height / 2);
}
};
main()
{
CRectangle rect;
CTriangle trgl;
CPolygon *ppoly1 = ▭
Page No : 116
CPolygon *ppoly2 = &trgl;
ppoly1->set_values(4, 5);
ppoly2->set_values(4, 5);
cout << "Area of Rectangle: " << ppoly1->area() << " sq. unit" << end
l;
cout << "Area of Triangle: " << ppoly2->area() << " sq. unit" << endl;
}
Output :
Area of Rectangle: 20 sq. unit
Page No : 117