syllabus programs :
using namespace std;
class Distance{
private:
int feet,inch;
public:
Distance(){
feet=10;
inch=10;
}
Distance(int feet1,int inch1){
feet=feet1;
inch=inch1;
}
~Distance(){
cout<<"destructor called"<<endl;
}
void displayDistance(){
cout<<"the distance is"<<endl;
cout<<feet<<"feet"<<inch<<"inch"<<endl;
}
void addDistance(Distance d1,Distance d2){
feet=d1.feet+d2.feet;
inch=d1.inch+d2.inch;
if(inch>12){
feet=d1.feet+d2.feet+(inch/12);
inch=inch%12;
}
}
};
int main(){
Distance d1,d2(20,20),d3;
d1.displayDistance();
d2.displayDistance();
d3.addDistance(d1,d2);
d3.displayDistance();
return 0;
}
3. Write a program for illustrating function overloading in adding the distance between
objects (use the above problem)
#include<iostream>
using namespace std;
class Distance{
private:
int feet,inch;
public:
Distance(){
feet=10;
inch=10;
}
Distance(int feet1,int inch1){
feet=feet1;
inch=inch1;
}
~Distance(){
cout<<"destructor called"<<endl;
}
void displayDistance(){
cout<<"the distance is"<<endl;
cout<<feet<<"feet"<<inch<<"inch"<<endl;
}
void addDistance(Distance d1,Distance d2){
feet=d1.feet+d2.feet;
inch=d1.inch+d2.inch;
if(inch>12){
feet=d1.feet+d2.feet+(inch/12);
inch=inch%12;
}
}
void addDistance(int feetq,int inchq){
feet=feet+feetq;
inch=inchq+inch;
if(inch>12){
feet=feet+(inch/12);
inch=inch%12;
}
}
};
int main(){
Distance d1,d2(20,20),d3,d4(15,15);
d1.displayDistance();
d2.displayDistance();
d3.addDistance(d1,d2);
d4.addDistance(30,30);
d3.displayDistance();
d4.displayDistance();
return 0;
}
22/12/2021
1.pre increment
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator ++(){
++a;
++b;
++c;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
++a1;
a1.displayA();
return 0;
}
2. post increment
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator ++(int i){
a++;
b++;
c++;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
a1++;
a1.displayA();
return 0;
}
3.pre decrement
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator --(){
--a;
--b;
--c;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
--a1;
a1.displayA();
return 0;
}
4.post decrement
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator --(int i){
a--;
b--;
c--;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
a1--;
a1.displayA();
return 0;
}
5.pre and post increment
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator ++(){
++a;
++b;
++c;
}
void operator ++(int i){
a++;
b++;
c++;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
++a1;
a1.displayA();
a1++;
return 0;
}
6.pre and post decrement
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator --(){
--a;
--b;
--c;
}
void operator --(int i){
a--;
b--;
c--;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
--a1;
a1.displayA();
a1--;
return 0;
}
7.pre post increment and pre post decrement
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator ++(){
++a;
++b;
++c;
}
void operator ++(int i){
a++;
b++;
c++;
}
void operator --(){
--a;
--b;
--c;
}
void operator --(int i){
a--;
b--;
c--;
}
};
int main(){
A a1;
a1.readA();
a1.displayA();
++a1;
a1.displayA();
a1++;
a1.displayA();
a1--;
a1.displayA();
--a1;
a1.displayA();
return 0;
}
8. assignment operator overloading
#include<iostream>
using namespace std;
class A{
private :
int a;
int b;
int c;
public:
void readA(){
cout<<"enter a,b,c values"<<endl;
cin>>a>>b>>c;
}
void displayA(){
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
void operator =(A a11){
a=a11.a;
b=a11.b;
c=a11.c;
}
};
int main(){
A a1,a2;
a1.readA();
a1.displayA();
a2=a1;
a2.displayA();
return 0;
}
practice programs for lab :
single inheritance
#include<conio.h>
class Data{
protected:
int a,b;
public:
void readData(){
cout<<"enter value of a,b"<<endl;
cin>>a>>b;
}
void displayData(){
cout<<"Entered Values are:"<<a<<" and "<<b<<endl;
}
};
class Add:public Data{
private:
int c;
public:
void add(){
c=a+b;
cout<<"Sum: "<<c;
}
};
int main(){
clrscr();
Add a1;
a1.readData();
a1.displayData();
a1.add();
getch();
return 0;
}
Multilevel hertiance
#include<iostream.h>
#include<conio.h>
class Data{
protected:
int a,b;
public:
void readData(){
cout<<"enter value of a,b"<<endl;
cin>>a>>b;
}
void displayData(){
cout<<"entered values are:"<<a<<" and "<<b<<endl;
}
};
class Sum:public Data{
protected:
int c;
public:
void sum(){
c=a+b;
}
};
class Print:public Sum{
public:
void print(){
cout<<"the sum is:"<<c<<endl;
}
};
int main(){
clrscr();
Print p1;
p1.readData();
p1.displayData();
p1.sum();
p1.print();
getch();
return 0;
}
Hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class Radius{
public:
int r;
public:
void readRadius(){
cout<<"Enter Radius:"<<endl;
cin>>r;
}
void displayRadius(){
cout<<"Value of Radius: "<<r<<endl;
}
};
class Circle:public Radius{
private:
int area;
public:
void circlearea(){
area=3.14*r*r;
}
void displayarea(){
cout<<"Area of circle is:"<<area<<endl;
}
};
class Sphere:public Radius{
private:
float s;
public:
void spherevolume(){
s=(1.3*3.14* r*r*r);
}
void displayspherevolume(){
cout<<"volume of sphere is:"<<s<<endl;
}
};
class Cylinder:public Radius{
private:
float v1,h;
public:
void readcylindervolume(){
cout<<"enter height of cylinder:"<<endl;
cin>>h;
}
void cylindervolume(){
v1=3.14*r*r*h;
}
void displaycylindervolume(){
cout<<"volume of cylinder is:"<<v1<<endl;
}
};
int main(){
clrscr();
Circle c1;
Sphere s1;
Cylinder y1;
c1.readRadius();
c1.displayRadius();
c1.circlearea();
c1.displayarea();
s1.readRadius();
s1.displayRadius();
s1.spherevolume();
s1.displayspherevolume();
y1.readRadius();
y1.readcylindervolume();
y1.displayRadius();
y1.cylindervolume();
y1.displaycylindervolume();
getch();
return 0;
}
6.
#include<iostream>
using namespace std;
class Area{
private:
int l,b,ar;
public:
Area(){
cout<<"enter l,b values";
cin>>l>>b;
}
void area(){
ar=l*b;
}
void displayArea(){
cout<<"area of rectangle is"<<ar<<endl;
}
};
int main(){
Area a1;
a1.area();
a1.displayArea();
return 0;
}
7. #include<iostream>
using namespace std;
class Area{
private:
int l,b,ar;
public:
Area(int x,int y){
l=x;
b=y;
}
void area(){
ar=l*b;
}
void displayArea(){
cout<<"area of rectangle is"<<ar<<endl;
}
};
int main(){
Area a1(2,8);
a1.area();
a1.displayArea();
return 0;
}
6. #include<iostream>
using namespace std;
class Area{
private:
int l,b,ar;
public:
Area(){
cout<<"enter l,b values";
cin>>l>>b;
}
void area(){
ar=l*b;
}
void displayArea(){
cout<<"area of rectangle is"<<ar<<endl;
}
};
int main(){
Area a1;
a1.area();
a1.displayArea();
return 0;
}
5 .
#include<iostream>
using namespace std;
class Area{
private:
int l,b,ar;
public:
void area(){
cout<<"area()";
int l=2,b=9,a;
a=l*b;
cout<<a;
}
void area(int l){
cout<<"area(int l)";
int a=l*l;
cout<<a;
}
void area(int l,int b){
cout<<"area(int l,int b)";
int a=l*b;
cout<<a;
}
};
int main(){
Area a1;
a1.area();
a1.area(50);
a1.area(50,56);
return 0;
}
4.
#include<iostream.h>
#include<conio.h>
class Add{
private:int a,b,c;
public:Add(){
cout<<"Enter the values of a,b";
cin>>a>>b;
}
void add(){
c=a*b;
}
void displayAdd(){
cout<<c;
}
};
int main()
{
clrscr();
Add a1;
cout<<endl;
a1.add();
a1.displayAdd();
cout<<endl;
Add a2;
a2.add();
cout<<endl;
a2.displayAdd();
getch();
return 0;
}