end of the program output: return 0; int i,n; int main .../**write a c++ program to count the the...
TRANSCRIPT
/**WRITE A C++ PROGRAM TO DISPLAY FIRST N NATURAL NUMNERS USING FOR
LOOP**/
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"enter the no. of natural numbers"<<endl;
cin>>n;
for(i=0;i<=n;i++)
{
cout<<i<<endl;
}
cout<<"end of the program";
return 0;
}
/**
output:
enter the no. of natural numbers
5
0
1
2
3
4
5
end of the program
**/
/**WRITE A C++ PROGRAM TO FIND FACTORIAL OF A NUMBR **/
#include<iostream>
using namespace std;
int main()
{
int i,num,fact=1;
cout<<"enter n value";
cin>>num;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<"the factorial of "<<num<<"is"<<fact<<endl;
return 0;
}
/**
OUTPUT:
enter n value
5
the factorial of 5is120
**/
/**WRITE A C++ PROGRAM TO DISPLAY SUM OF N NATURAL NUMBERS**/
#include<iostream>
using namespace std;
int main()
{
int i,n,sum=0;
cout<<"enter n value";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout<<"the sum of"<< n <<"no. is"<<sum;
return 0;
}
/**
OUTPUT:
enter n value
10
the sum of10no. is55
**/
/**WRITE A C++ PROGRAM TO DISPLAY FIRST N NATURAL NUMNERS USING WHILE
LOOP**/
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"enter n value";
cin>>n;
i=0;
cout<<"the natural no. are\n";
while(i<=n)
{
cout<<i<<"\t";
i++;
}
return 0;
}
/**
OUTPUT:
enter n value
10
the natural no. are
0 1 2 3 4 5 6 7 8 9
10
**/
/**write a c++ program to count the the no. of digits and sum of the
digits in a given number**/
#include<iostream>
using namespace std;
int main()
{
int num,count=0,sum=0,rem;
cout<<"enter number\n";
cin>>num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
count++;
}
cout<<"the no. of digits:"<<count<<endl;
cout<<"the sum is:"<<sum<<endl;
return 0;
}
/**
oupur:
enter number
745896
the no. of digits:6
the sum is:39
**/
/**write a c++ program to count the no. of digits and to multiplythe
digits of a given number**/
#include<iostream>
using namespace std;
int main()
{
int num,count=0,mul=1,rem;
cout<<"enter number\n";
cin>>num;
while(num!=0)
{
rem=num%10;
mul=mul*rem;
num=num/10;
count++;
}
cout<<"the no. of digits:"<<count<<endl;
cout<<"the mul is:"<<mul<<endl;
return 0;
}
/**
output:
enter number
25
the no. of digits:2
the mul is:10
**/
/** write a c++ progrma to print the n natural numbers using do-while
loop**/
#include<iostream>
using namespace std;
int main()
{
int i=1,n;
cout<<"enter n value:";
cin>>n;
do
{
cout<<i<<'\n';
i++;
}while(i<=n);
cout<<"end";
return 0;
}
/**
OUTPUT:
enter n value:10
1
2
3
4
5
6
7
8
9
10
end
**/
/** write a c++ progrma to find the number of students who have their age
less than 25 and weight less than 50**/
#include<iostream>
using namespace std;
int main()
{
int n,age,weight,count,i=1;
cout<<"enter number of students";
cin>>n;
cout<<"enter student details"<<endl;
do
{
cout<<"age"<<"\tweight\n";
cin>>age>>weight;
if(age<25&&weight<50)
{
count++;
}
i++;
}
while(i<=n);
cout<<"the count is:"<<count<<endl;
return 0;
}
/**
output:
enter number of students
4
enter student details
age weight
48 45
age weight
48 78
age weight
22 42
age weight
78 95
the count is:1
**/
/** write a c++ progrma to find whether the given number is ARMSTRONG
number or not**/
#include<iostream>
using namespace std;
int main()
{
int sum=0,rem,n,temp,count;
cout<<"enter the number";
cin>>n;
temp=n;
while(temp!=0)
{
rem=temp%10;
sum=sum+rem*rem*rem;
temp=temp/10;
count++;
}
if(n==sum)
cout<<n<<" is armstrong Number";
else
cout<<n<<" is not a armstrong Number";
return 0;
}
/**
OUTPUT:
enter the number
153
153 is armstrong Number
**/
/** write a c++ progrma to find whether the given number is STRONG number
or not**/
#include<iostream>
using namespace std;
int main()
{
int fact=1,rem,n,temp,i,sum=0;
cout<<"enter the number";
cin>>n;
temp=n;
while(temp!=0)
{
rem=temp%10;
for(i=1;i<=rem;i++)
fact=fact*i;
sum=sum+fact;
temp=temp/10;
fact=1;
}
if(sum==n)
cout<<n<<" is strong Number";
else
cout<<n<<" is not a strong Number";
}
/**
OUTPUT:
enter the number
145
145 is strong Number
**/
/** write a c++ progrma to demonstrate the working of classes and
objects**/
#include<iostream>
using namespace std;
class person
{
public:
char name[30];
int id;
};
int main()
{
person p;
cout<<"enter person details"<<endl;
cin>>p.name;
cin>>p.id;
cout<<"the person details are"<<endl;
cout<<p.name<<endl;
cout<<p.id;
return 0;
}
/**
OUTPUT:
enter person details
EEE
1241
the person details are
EEE
1241
**/
#include<iostream>
using namespace std;
class Box
{
public:
int volume(int) ;
double volume(double,int);
long volume(long,int,int);
};
int Box::volume(int s)
{
return(s*s*s);
}
double Box::volume(double r,int h)
{
return(3.14*r*r*h);
}
long Box::volume(long l,int b,int d)
{
return(l*b*d);
}
int main()
{
Box b;
cout<<"Box 1 Volume:"<<b.volume(10)<<endl;
cout<<"Box 2 Volume:"<<b.volume(5.25,3)<<endl;
cout<<"Box 3 Volume:"<<b.volume(100l,5,6)<<endl;
return 0;
}
/**
OUTPUT:
Box 1 Volume:1000
Box 2 Volume:259.639
Box 3 Volume:3000
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF SINGLE
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
int main()
{
A obja;
B objb;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.setB(20);
objb.showA();
objb.showB();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF MULTIPLE
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class C:public A,public B
{
int c;
public:
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setB(20);
objb.showB();
cout<<"Using Class C"<<endl;
objc.setA(10);
objc.showA();
objc.setB(20);
objc.showB();
objc.setC(30);
objc.showC();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
B=20
Using Class C
A=10
B=20
C=30
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF HIERARCHICAL
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class C:public A
{
int c;
public:
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.showA();
objb.setB(20);
objb.showB();
cout<<"Using Class C"<<endl;
objc.setA(10);
objc.showA();
objc.setC(30);
objc.showC();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
Using Class C
A=10
C=30
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF SINGLE LEVEL
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
int main()
{
A obja;
B objb;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.setB(20);
objb.showA();
objb.showB();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF MULTI LEVEL
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class C:public B
{
int c;
public:
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.showA();
objb.setB(20);
objb.showB();
cout<<"Using Class C"<<endl;
objc.setA(10);
objc.showA();
objb.setB(20);
objb.showB();
objc.setC(30);
objc.showC();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
Using Class C
A=10
B=20
C=30
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF HYBRID
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class D
{
int d;
public:
void setD(int w)
{
d=w;
}
void showD()
{
cout<<"D="<<d<<endl;
}
};
class C:public B,public D
{
int c;
public:
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
D objd;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.showA();
objb.setB(20);
objb.showB();
cout<<"Using Class D"<<endl;
objd.setD(40);
objd.showD();
cout<<"Using Class C"<<endl;
objc.setA(10);
objc.showA();
objc.setB(20);
objc.showB();
objc.setC(30);
objc.showC();
objc.setD(40);
objc.showD();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
Using Class D
D=40
Using Class C
A=10
B=20
C=30
D=40
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF MULTI PATH
INHERITANCE**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class C:public A
{
int c;
public:
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
class D:public B,public C
{
int d;
public:
void setD(int w)
{
d=w;
}
void showD()
{
cout<<"D="<<d<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
D objd;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.showA();
objb.setB(20);
objb.showB();
cout<<"Using Class C"<<endl;
objc.setA(10);
objc.showA();
objc.setC(30);
objc.showC();
cout<<"Using Class D"<<endl;
//objd.setA(10);
//objd.showA();
objd.setB(20);
objd.showB();
objd.setC(30);
objd.showC();
objd.setD(40);
objd.showD();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
Using Class C
A=10
C=30
Using Class D
B=20
C=30
D=40
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF VIRTUAL BASE
CLASS**/
#include<iostream>
using namespace std;
class A
{
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:virtual public A
{
int b;
public:
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class C:public virtual A
{
int c;
public:
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
class D:public B,public C
{
int d;
public:
void setD(int w)
{
d=w;
}
void showD()
{
cout<<"D="<<d<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
D objd;
cout<<"Using Class A"<<endl;
obja.setA(10);
obja.showA();
cout<<"Using Class B"<<endl;
objb.setA(10);
objb.showA();
objb.setB(20);
objb.showB();
cout<<"Using Class C"<<endl;
objc.setA(10);
objc.showA();
objc.setC(30);
objc.showC();
cout<<"Using Class D"<<endl;
objd.setA(10);
objd.showA();
objd.setB(20);
objd.showB();
objd.setC(30);
objd.showC();
objd.setD(40);
objd.showD();
return 0;
}
/**
OUTPUT:
Using Class A
A=10
Using Class B
A=10
B=20
Using Class C
A=10
C=30
Using Class D
A=10
B=20
C=30
D=40
**/
#include<iostream>
using namespace std;
class A
{
public:
int a;
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:public A
{
public:
int b;
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
class C:public B
{
public:
int c;
void setC(int z)
{
c=z;
}
void showC()
{
cout<<"C="<<c<<endl;
}
};
int main()
{
A obja;
B objb;
C objc;
cout<<"Calling Class A Methods"<<endl;
obja.setA(10);
obja.showA();
cout<<"Calling Class B Methods"<<endl;
objb.setA(10);
objb.showA();
objb.setB(20);
objb.showB();
cout<<"Calling Class A & B Methods"<<endl;
objc.setA(10);
objc.setB(20);
objc.setC(30);
objc.showA();
objc.showB();
objc.showC();
return 0;
}
/**
OUTPUT:
Calling Class A Methods
A=10
Calling Class B Methods
A=10
B=20
Calling Class A & B Methods
A=10
B=20
C=30
**/
#include<iostream>
using namespace std;
class A
{
public:
int a;
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:private A
{
public:
int b;
void setB(int y)
{
b=y;
}
void showB()
{
cout<<"B="<<b<<endl;
}
};
int main()
{
B objb;
cout<<"Using Class B"<<endl;
//objb.setA(10);
objb.setB(20);
//objb.showA();
objb.showB();
return 0;
}
/**
OUTPUT:
Using Class B
B=20
**/
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void setA(int x)
{
a=x;
}
void showA()
{
cout<<"A="<<a<<endl;
}
};
class B:protected A
{
public:
int b;
void setB(int y)
{
setA(10);
b=y;
}
void showB()
{
showA();
cout<<"B="<<b<<endl;
}
};
int main()
{
B objb;
cout<<"Using Class B"<<endl;
//objb.setA(10);
objb.setB(20);
//objb.showA();
objb.showB();
return 0;
}
/**
OUTPUT:
Using Class B
A=10
B=20
**/
#include<iostream>
using namespace std;
class A
{
public:
int a;
A()
{
a=10;
}
};
class B:private A
{
public:
A::a;
};
class C:private B
{
public:
void show()
{
cout<<"A="<<a<<endl;
}
};
int main()
{
C objc;
cout<<"Using Class C"<<endl;
objc.show();
return 0;
}
/**
OUTPUT:
Using Class C
A=10
**/
#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
cout<<"Base class Constructor"<<endl;
}
~Base()
{
cout<<"Base class Destructor"<<endl;
}
};
class Derived:Base
{
public:
Derived()
{
cout<<"Derived class Constructor"<<endl;
}
~Derived()
{
cout<<"Derived class Destructor"<<endl;
}
};
int main()
{
Derived obj;
return 0;
}
/**
OUTPUT:
Base class Constructor
Derived class Constructor
Derived class Destructor
Base class Destructor
**/
#include<iostream>
using namespace std;
class Base
{
public:
Base(int x)
{
cout<<"Base class Constructor"<<endl;
cout<<"X="<<x<<endl;
}
~Base()
{
cout<<"Base class Destructor"<<endl;
}
};
class Derived:Base
{
public:
Derived(int p,int q):Base(q)
{
cout<<"Derived class Constructor"<<endl;
cout<<"P="<<p<<"\n"<<"Q="<<q<<endl;
}
~Derived()
{
cout<<"Derived class Destructor"<<endl;
}
};
int main()
{
Derived obj(10,20);
return 0;
}
/**
OUTPUT:
Base class Constructor
X=20
Derived class Constructor
P=10
Q=20
Derived class Destructor
Base class Destructor
**/
#include<iostream>
using namespace std;
class Space
{
int x,y,z;
public:
void getdata(int,int,int);
void operator-();
void display();
};
void Space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void Space::operator-()
{
x=-x;
y=-y;
z=-z;
}
void Space::display()
{
cout<<"X="<<x<<"\n"<<"Y="<<y<<"\n"<<"Z="<<z<<endl;
}
int main()
{
Space s;
s.getdata(10,-12,13);
s.display();
-s;
s.display();
return 0;
}
/**
OUTPUT:
X=10
Y=-12
Z=13
X=-10
Y=12
Z=-13
**/
#include<iostream>
using namespace std;
class Box
{
int a,b,c;
public:
Box()
{
}
Box(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
Box operator+(Box &);
int volume();
};
Box Box::operator+(Box &d)
{
Box temp;
temp.a=a+d.a;
temp.b=b+d.b;
temp.c=c+d.c;
return temp;
}
int Box::volume()
{
return a*b*c;
}
int main()
{
Box b1=Box(10,20,30);
cout<<"Box1 Volume:"<<b1.volume()<<endl;
Box b2=Box(1,2,3);
cout<<"Box2 Volume:"<<b2.volume()<<endl;
Box b3;
b3=b1+b2;
//b3=b1.operator+(b2);
cout<<"Box3 Volume:"<<b3.volume()<<endl;
return 0;
}
/**
OUTPUT:
Box1 Volume:6000
Box2 Volume:6
Box3 Volume:7986
**/
#include<iostream>
using namespace std;
class Box
{
int a,b,c;
public:
Box()
{
}
Box(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
friend Box operator+(Box,Box);
int volume();
};
Box operator+(Box x,Box d)
{
Box temp;
temp.a=x.a+d.a;
temp.b=x.b+d.b;
temp.c=x.c+d.c;
return temp;
}
int Box::volume()
{
return a*b*c;
}
int main()
{
Box b1=Box(10,20,30);
cout<<"Box1 Volume:"<<b1.volume()<<endl;
Box b2=Box(1,2,3);
cout<<"Box2 Volume:"<<b2.volume()<<endl;
Box b3;
b3=operator+(b1,b2);
cout<<"Box3 Volume:"<<b3.volume()<<endl;
return 0;
}
/**
OUTPUT:
Box1 Volume:6000
Box2 Volume:6
Box3 Volume:7986
**/
#include<iostream>
using namespace std;
class Space
{
int x,y,z;
public:
void getdata(int,int,int);
friend Space operator-(Space &);
void display();
};
void Space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
Space operator-(Space &s)
{
Space temp;
temp.x=-s.x;
temp.y=-s.y;
temp.z=-s.z;
return temp;
}
void Space::display()
{
cout<<"X="<<x<<"\n"<<"Y="<<y<<"\n"<<"Z="<<z<<endl;
}
int main()
{
Space s1,s2;
s1.getdata(10,-12,13);
s1.display();
s2=operator-(s1);
s2.display();
return 0;
}
/**
OUTPUT:
X=10
Y=-12
Z=13
X=-10
Y=12
Z=-13
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF FREIND FUNCTION**/
#include<iostream>
using namespace std;
class student
{
char name[30];
int id;
public:
friend void display(student);
};
void display(student s)
{
cout<<"enter student details"<<endl;
cout<<"enter student name"<<endl;
cin>>s.name;
cout<<"enter student id"<<endl;
cin>>s.id;
cout<<"the student details are"<<endl;
cout<<"name:"<<s.name<<endl;
cout<<"id:"<<s.id<<endl;
}
int main()
{
student s;
display(s);
return 0;
}
/**
OUTPUT:
enter student details
enter student name
EEE
enter student id
123
the student details are
name:EEE
id:123
**/
/**WRITE A C++ PROGRAM TO ACCESS PRIVATE MEMMERS OF DIFFERENT CLASSES
USING FREIND FUNCTION**/
#include<iostream>
using namespace std;
class B;
class A
{
private:
int numA;
public:
A():numA(10)
{
}
friend int add(A,B);
};
class B
{
private:
int numB;
public:
B():numB(20)
{
}
friend int add(A,B);
};
int add (A obja,B objb)
{
return(obja.numA+objb.numB);
}
int main()
{
A obja;
B objb;
cout<<"the addition is:"<<add(obja,objb)<<endl;
return 0;
}
/**
OUTPUT:
the addition is:30
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF FREIND CLASS**/
#include<iostream>
using namespace std;
class B;
class A
{
private:
int numA;
public:
A():numA(10)
{
}
friend B;
friend int add(A,B);
};
class B
{
private:
int numB;
public:
B():numB(20)
{
}
friend int add(A,B);
int sub(A,B);
};
int add (A obja,B objb)
{
return(obja.numA+objb.numB);
}
int B::sub(A obja,B objb)
{
return(obja.numA-objb.numB);
}
int main()
{
A obja;
B objb;
cout<<"the addition is:"<<add(obja,objb)<<endl;
cout<<"the subtraction is"<<objb.sub(obja,objb)<<endl;
return 0;
}
/**
OUTPUT:
the addition is:30
the subtraction is-10
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF TEMPLATE FUNCTION**/
#include<iostream>
using namespace std;
template<class T>void add(T a,T b)
{
T sum;
sum=a+b;
cout<<"the sum is:"<<sum<<endl;
}
int main()
{
add(10,20);
add(20.5,30.2);
return 0;
}
/**
OUTPUT:
the sum is:30
the sum is:50.7
**/
/**WRITE A C++ PROGRAM TO DEMONSTRATE THE WORKING OF TEMPLATE FUNCTION
OVERLOADING**/
#include<iostream>
using namespace std;
template<class X,class Y>void display(X a,Y b)
{
cout<<"A="<<a<<" " <<"B="<<b<<endl;
}
void display(int a,int b)
{
cout<<"A="<<a<<" " <<"B="<<b<<endl;
}
int main()
{
display(10,20);
display(20,30.5);
display("hello","world");
display(20,"hello");
return 0;
}
/**
OUTPUT:
A=10 B=20
A=20 B=30.5
A=hello B=world
A=20 B=hello
**/
#include<iostream>
#include<string.h>
using namespace std;
class Person
{
char name[30];
int age;
public:
Person(char *s,int a)
{
strcpy(name,s);
age=a;
}
Person greater(Person);
void display();
};
Person Person::greater(Person x)
{
if(x.age>=age)
return x;
else
return *this;
}
void Person::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
}
int main()
{
Person p1("JOHN",39),p2("ABDUL",28),p3("RAM",40);
Person p=p1.greater(p3);
cout<<"The Elder Person is:"<<endl;
p.display();
p=p1.greater(p2);
cout<<"The Elder Person is:"<<endl;
p.display();
return 0;
}
/**
OUTPUT:
The Elder Person is:
Name:RAM
Age:40
The Elder Person is:
Name:JOHN
Age:39
**/
#include<iostream>
using namespace std;
class Base
{
public:
void display()
{
cout<<"Base Class Display"<<endl;
}
virtual void show()
{
cout<<"Base Class Show"<<endl;
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"Derived Class Display"<<endl;
}
void show()
{
cout<<"Derived Class Show"<<endl;
}
};
int main()
{
Base b;
Base *bptr;
Derived d;
bptr=&b;
bptr->display();
bptr->show();
bptr=&d;
bptr->display();
bptr->show();
return 0;
}
/**
OUTPUT:
Base Class Display
Base Class Show
Base Class Display
Derived Class Show
**/
#include<iostream>
#include<string.h>
using namespace std;
class Media
{
protected:
char tittle[30];
float price;
public:
Media(char *s,float p)
{
strcpy(tittle,s);
price=p;
}
virtual void display()=0;
};
class Book:public Media
{
int pages;
public:
Book(char *s,float p,int a):Media(s,p)
{
pages=a;
}
void display();
};
class Tape:public Media
{
float time;
public:
Tape(char *s,float p,float t):Media(s,p)
{
time=t;
}
void display();
};
void Book::display()
{
cout<<"The Book Details are:"<<endl;
cout<<"Book Tittle:"<<tittle<<endl;
cout<<"Book Price:"<<price<<endl;
cout<<"Book Pages:"<<pages<<endl;
}
void Tape::display()
{
cout<<"The Tape Details are:"<<endl;
cout<<"Tape Tittle:"<<tittle<<endl;
cout<<"Tape Price:"<<price<<endl;
cout<<"Tape Time(ms):"<<time<<endl;
}
int main()
{
char *tittle=new char[30];
float price;
float time;
int pages;
cout<<"Enter Book Details:"<<endl;
cout<<"Tittle:";cin>>tittle;
cout<<"Price:";cin>>price;
cout<<"Pages:";cin>>pages;
Book b(tittle,price,pages);
cout<<"Enter Tape Details:"<<endl;
cout<<"Tittle:";cin>>tittle;
cout<<"Price:";cin>>price;
cout<<"Time(ms):";cin>>time;
Tape tp(tittle,price,time);
Media *list[2];
list[0]=&b;
list[0]->display();
list[1]=&tp;
list[1]->display();
return 0;
}
/**
OUTPUT:
Enter Book Details:
Tittle:JAVA
Price:152.23
Pages:500
Enter Tape Details:
Tittle:RADIO
Price:126.36
Time(ms):250
The Book Details are:
Book Tittle:JAVA
Book Price:152.23
Book Pages:500
The Tape Details are:
Tape Tittle:RADIO
Tape Price:126.36
Tape Time(ms):250
**/