Search This Blog

Powered by Blogger.

Pages

  • Home

CPLUS±

/*WRITE A PROGRAM TO SHOW THE USE OF TEMPLATE*/

#include<iostream.h>
#include<conio.h>
template<class T>
class data
{
public:
data(T c)
{
cout<<"\nc="<<c;
cout<<"\nsize in bytes="<<sizeof(c);
}
};
void main()
{
data<char>h('A');
data<int>i(10);
data<float>j(1.2);
getch();

}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW FUNCTION TEMPLATE*/

#include<iostream.h>
#include<conio.h>
template<classe>
void exchange(e&a,e&b)
{
et=a;
a=b;
b=t;
}
void main()
{
int x=5,y=8;
cout<<"\nBefore exchange"<<x<<y;
exchange(x,y);
cout<<"\nAfter exchange"<<x<<y;
getch();

}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM USING VIRTUAL FUNCTIONS*/

#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\nDisplay base";
}
virtual void show()
{
cout<<"\nShow base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\nDisplay derived";
}
void show()
{
cout<<"\nShow derived";
}
};
void main()
{
base b;

derived d;
base*bptr;
cout<<"\nbptr points to base";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\nbptr points to derived";
bptr=&d;
bptr->display();
bptr->show();
return0;
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW USE OF THIS POINTER*/

#include<iostream.h>
#include<conio.h>
class person
{
char name[50];
int age;
public:
person(char*s,inta)
{
strcpy(name,s);
age=a;
}
person greater(person&x)
{
if(x.age>=age)
return x;
else
return*this;
}
void display()
{
cout<<"\nName:"<<name;
cout<<"\nAge:"<<age;
}
};
void main()
{
person p1("John",37);
person p2("Ram",29);

person p3("Shyam",43);
person p=p1.greater(p3);
cout<<"\nElder person is:";
p.display();
p=p1.greater(p2);
cout<<"\nElder person is:";
p.display();
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW OVERLOADING OF BINARY OPERATOR*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex
{
float x, y;
public:
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";
}
void main()

{
complex c1, c2, c3;
c1=complex(2.5,1.5);
c2=complex(3.2,1.7);
c3=c1+c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c3=";
c3.display();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW HIERARCHICAL INHERITANCE*/

#include<iostream.h>
#include<conio.h>
#include<math.h>
class bank
{
char name[20];
int ac_no;
float r;
float t;
int ac_type;
public:
float p;
float ret_si();
float ret_ci();
int chk_ac();
char show_name();
int show_acno();
void detail(void);
void input(void);
};
class current:public bank
{
float s;
float bal;
public:
void si(void);
void display(void);
};
class saving:public bank

{
float c;
float total;
public:
void ci(void);
void display1(void);
};
//-------------------------------------------------------
int bank::chk_ac()
{
cout<<"\n\t\DOLLAR BANK";
cout<<"\n\n\t##Main menu##";
cout<<"\nPress1 for current account";
cout<<"\nPress2 for saving account";
cout<<"\n\nEnter Customer account type:";
cin>>ac_type;
cout<<"\n-----------------------------------------------------------------\n";
return ac_type;
}
void bank::detail(void)
{
cout<<"\nEnter customer name:";
cin>>name;
cout<<"\nEnter customer Account Number:";
cin>>ac_no;
cout<<"\n------------------------------------------------------------------\n";
}
char bank::show_name()
{
cout<<"\nName of Customer:"<<name;
}
int bank::show_acno()
{
cout<<"\nCustomer's accountno:"<<ac_no;
}
void bank::input(void)
{
cout<<"\n\nEnter principal:";
cin>>p;
cout<<"\nEnter Rate:";
cin>>r;
cout<<"\nEnter Time in Years:";
cin>>t;
}
//--------------------------------------------------------------
float bank::ret_si()
{
return t*r;
}
void current::si(void)
{
s=(p*ret_si())/100;
bal=s+p;
}
void current::display(void)
{
show_name();
show_acno();
cout<<"\nAccount type:Current Account";
cout<<"\nAmount recieved from Customer:"<<p;
cout<<"\nInterest on amount:"<<s;
cout<<"\nBalance:"<<bal;
}
//-----------------------------------------------------
float bank::ret_ci()
{
return p*(pow((1+r/100),t));
}
void saving::ci(void)
{
c=ret_ci()-p;
total=p+c;
}
void saving::display1(void)
{
show_name();
show_acno();
cout<<"\nAccount type:Saving Account";
cout<<"\nAmount recieved:"<<p;
cout<<"\nInterest on amount:"<<c;
cout<<"\ntotal amount:"<<total;
}
float main()
{
bank b;
saving s;
current c;
int x;
x=b.chk_ac();
if(x==1)
{
c.detail();
c.input();
c.si();
c.display();
}
if(x==2)
{
s.detail();
s.input();
s.ci();
s.display1();
}
return 0;
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO CALL A MEMBER FUNCTION OF A CLASS USING A NON MEMBER FUNCTION*/

#include<iostream.h>
#include<conio.h>
classmember
{
inta,b;
public:
voidputdata(intnum1,intnum2);
voidgetdata();
};
voidmember::putdata(intnum1,intnum2)
{
a=num1;
b=num2;
}
voidmember::getdata()
{
cout<<"a="<<a;
cout<<"\nb="<<b;
}
voiddisplay()
{
memberm;
m.putdata(10,20);
m.getdata();
}
voidmain()
{
display();

}

----------------------------------------------------------
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW HYBRID INHERITANCE*/

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollnumber;
public:
void getnumber(inta)
{
rollnumber=a;
}
void putnumber()
{
cout<<"\nRollno="<<rollnumber;
}
};
class test:public student
{
protected:
int part1, part2;
public:
void getmarks(intx,inty)
{
part1=x, part2=y;
}
void putmarks()
{
cout<<"\nMarks obtained";
cout<<"\nPart1="<<part1;

cout<<"\nPart2="<<part2;
}
};
class sports
{
protected:
int score;
public:
void getscore(ints)
{
score=s;
}
void putscore()
{
cout<<"\nSports marks="<<score;
}
};
class result:public test,public sports
{
int total;
public:
void display();
};
void result::display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\nTotal score="<<total;
}
void main()
{
result student1;
student1.getnumber(108);
student1.getmarks(60,70);
student1.getscore(50);
student1.display();
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW MULTILEVEL INHERITANCE*/

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getnumber(int);
void putnumber();
};
void student::getnumber(inta)
{
rollno=a;
}
void student::putnumber()
{
cout<<"Rollno="<<rollno;
}
class test:public student
{
protected:
int sub1;
int sub2;
public:
void getmarks(int,int);
void putmarks();
};
void test::getmarks(intx,inty)
{

sub1=x;
sub2=y;
}
void test::putmarks()
{
cout<<"\nMarks in sub1="<<sub1;
cout<<"\nMarks in sub2="<<sub2;
}
class result:public test
{
protected:
int total;
public:
void display();
};
void result::display()
{
total=sub1+sub2;
putnumber();
putmarks();
cout<<"\nTotal marks="<<total;
}
void main()
{
resultR1;
R1.getmarks(70,90);
R1.getnumber(150);
R1.display();
getch();
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW MULTIPLE INHERITANCE*/

#include<iostream.h>
#include<conio.h>
class A
{
protected:
int m;
public:
void getm(int);
};
class B
{
protected:
int n;
public:
void getn(int);
};
class C:public A,public B
{
public:
void display(void);
};
void A::getm(intx)
{
m=x;
}
void B::getn(inty)
{
n=y;
}

void C::display()
{
cout<<"\nm="<<m;
cout<<"\nn="<<n;
cout<<"\nm*n="<<m*n;
}
void main()
{
C c;
c.getm(10);
c.getn(20);
c.display();
return0;
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW SINGLE INHERITANCE*/


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
class D:public B
{
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()

{}
void D::mul()
{
c=b*get_a();
}
void D::display()
{
cout<<"\na="<<get_a();
cout<<"\nb="<<b;
cout<<"\nc="<<c;
}
int main()
{
clrscr();
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
cout<<"\nafter b=20";
d.b=20;
d.mul();
d.display();
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW DESTRUCTOR*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
intcount=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\nnumber of object created"<<count;
}
~alpha()
{
cout<<"\nnumber of object destroy"<<count;
count--;
}
};
int main()
{
clrscr();
cout<<"\nENTER MAIN";
alpha A1,A2,A3,A4;
{
cout<<"\nENTER BLOCK1";
alpha A5;
}
{
cout<<"\nENTER BLOCK2";
alpha A6;

}
cout<<"\nRE-ENTER MAIN";
getch();
}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW COPY CONSTRUCTOR*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class code
{
int id;
public:
code(int a)
{
id=a;
}
code(code&x)
{
id=x.id;
}
void display()
{
cout<<id;
}
};
void main()
{
clrscr();
code A(100);
code B(A);
code C=A;
cout<<"\n id of A";
A.display();
cout<<"\n id of B";B.display();
cout<<"\n id of C";C.display();

getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO ADD TWO COMPLEX NUMBERS USING CONSTRUCTOR*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class complex
{

float x, y;
public:
complex()
{
x=y=0;
}
complex(float a)
{
x=y=a;
}
complex(float real,float img)
{
x=real;
y=img;
}
friend complex sum(complex,complex);
friend void show(complex);
};
complex sum(complexc1,complexc2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void show(complexc)
{
cout<<c.x<<"\t"<<c.y;
}
void 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);
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SWAP PRIVATE DATA OF TWO CLASSES USING REFERENCE OF THE OBJECT WITH THE HELP OF FRIEND FUNCTION*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class class2;
class class1
{
int var1;
public:
void set value(inta)
{
var1=a;
}
void display()
{
cout<<var1;
};
friend void swap(class1&obj1,class2&obj2);
};
class class2
{
int var2;
public:
void set value(inta)
{
var2=a;
}
void display()
{

cout<<var2;
};
friend void swap(class1&obj1,class2&obj2);
};
void swap(class1&obj1,class2&obj2)
{
int temp;
temp=obj1.var1;
obj1.var1=obj2.var2;
obj2.var2=temp;
}
void main()
{
class1 c1;
class2 c2;
c1.setvalue(100);
c2.setvalue(200);
cout<<"before swapping";
c1.display();
c2.display();
swap(c1,c2);
cout<<"after swapping";
c1.display();
c2.display();
getch();
}


----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO FIND MEAN OF TWO NUMBERS USING FRIEND FUNCTION*/

#include<conio.h>
#include<iostream.h>
class sample
{
int a;
int b;
public:
void set value()
{
a=25;
b=40;
}
friend float mean(samples);
};
float mean(samples)
{
return float(s.a+s.b)/2;
}
void main()
{
sample x;
x.setvalue();
cout<<"mean="<<mean(x);

}

----------------------------------------------------------
OUTPUT:-
Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO FIND MAXIMUM OF TWO NUMBERS BELONGING TO TWO DIFFERENT CLASSES USING FRIEND FUNCTION*/

#include<conio.h>
#include<iostream.h>
class XYZ;
class ABC
{
int x;
public:
void setvalue(inti)
{
x=i;
}
friend void max(ABC,XYZ);
};
class XYZ
{
inty;

public:
void set value(intj)
{
y=j;
}
friend void max(ABC,XYZ);
};
void max(ABC a,XYZ b)
{
if(a.x>=b.y)
cout<<"maximum value is"<<a.x;
else
cout<<"maximum value is"<<b.y;
}
void main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(abc,xyz);
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SWAP PRIVATE DATA MEMBER OF TWO DIFFERENT CLASSES USING FRIEND FUNCTION*/

#include<conio.h>
#include<iostream.h>
class two;
class one
{
intx;
public:
void set value(inti)
{
x=i;
}
void display()
{
cout<<"x="<<x;
};
friend void swap(one,two);
};
classtwo
{
inty;
public:
void set value(intj)
{
y=j;
}
void display()
{
cout<<"y="<<y;
};

friend void swap(one,two);
};
void swap(onea,twob)
{
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"x="<<a.x<<"y="<<b.y;
}
voidmain()
{
one on;
two tw;
cout<<"\nBefore swapping";
on.setvalue(10);
tw.setvalue(20);
on.display();
tw.display();
cout<<"\nAfter swapping";
swap(on,tw);
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
1 comments
/*WRITE A PROGRAM TO ADD TWO TIMES*/

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class time
{
int hours;
int minutes;
public:
void gettime(inth,intm)
{
hours=h;
minutes=m;
}
voidputtime()
{
cout<<"\nhours="<<hours<<"minutes="<<minutes;
}
void sum(time,time);
};
void time::sum(timet1,timet2)
{
cout<<"\n\nAfter adding two times";
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
void main()
{
time t1, t2, t3;
t1.gettime(2,45);

t1.puttime();
t2.gettime(3,45);
t2.puttime();
t3.sum(t1,t2);
t3.puttime();
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO ENTER NAME AGE SALARY OF 5 EMPLOYEES USING ARRAY OF OBJECTS*/

#include<iostream.h>
#include<conio.h>
class employee
{
char name[20];
int age, salary;
public:
void getdata();
void putdata();
};
const int size=5;
void employee::getdata()
{
cout<<"\nenter name=";
cin>>name;
cout<<"\nenter age=";
cin>>age;
cout<<"\nsalary=";
cin>>salary;
}
void employee::putdata()
{
cout<<"\ndata";
cout<<"\nname is="<<name;
cout<<"\nage is="<<age;
cout<<"\nsalary is="<<salary;
}
void main()
{
employee emp[5];
for(inti=0;i<size;i++)
emp[i].getdata();
for(intj=0;j<size;j++)
emp[j].putdata();
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW STATIC MEMBER FUNCTION*/


#include<conio.h>
#include<iostream.h>
class item
{
int code;
static int count;
public:
void setcode()
{
code=count++;
}
void showcode()
{
cout<<"\nobject number"<<code;
}
static void showcount()
{
cout<<"\ncount"<<count;
}
};
int item::count;
void main()
{
item a, b;
a.setcode();
b.setcode();
item::showcount();
item c;
c.setcode();

item::showcount();
a.showcode();
b.showcode();
c.showcode();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW STATIC DATA MEMBER*/

#include<conio.h>
#include<iostream.h>
class item
{
int number;
static int count;
public:
void getdata()
{
count++;
}
voidgetcount()
{
cout<<"\ncount="<<count;
}
};
int item::count;
voidmain()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata();
b.getdata();
c.getdata();
cout<<"\nafter reading data";
a.getcount();
b.getcount();

c.getcount();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO SHOW SCOPE RESOLUTION OPERATOR*/


#include<iostream.h>
#include<conio.h>
int a=10;
void main()
{
int a=20;
{
int b;
b=a;
int a=30;
cout<<"\na="<<a;
cout<<"\nb="<<b;
cout<<"\na="<<::a;
}
cout<<"\na="<<a;
getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO FIND LARGEST OF TWO NUMBERS USING CLASS AND NESTING THE FUNCTION*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class large
{
int a, b;
public:
void getdata()
{
cout<<"enter two numbers";
cin>>a>>b;
}
void largest()
{
if(a>b)
cout<<"largest number="<<a;
else
cout<<"largest number="<<b;
}
void putdata()
{
largest();
}
};
void main()
{
large obj;
obj.getdata();

obj.putdata();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO FIND VOLUME OF CYLINDER CUBE AND CUBOID USING FUNCTION
OVERLOADING*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void volume(inta,intb);
void volume(inta,intb,intc);
void volume(inta);
voidmain()
{
volume(2,4);
volume(2,4,6);
volume(3);
getch();
}
void volume(inta,intb)
{
float vol;
floatpie=3.14;
vol=a*b*pie;
cout<<"\nthe volume of cylinderis"<<vol;
}
void volume(inta,intb,intc)
{
int vol;
vol=a*b*c;
cout<<"\nthe volume of cuboidis"<<vol;
}
void volume(inta)
{
int vol;
vol=a*a*a;

cout<<"\nthe volume of cubeis"<<vol;
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO FIND SIMPLE INTEREST USING DEFAULT ARGUMENT*/
       
 #include<iostream.h>
#include<conio.h>
#include<stdio.h>
float interest(intp,intt,float r=0.15)
{
 return (p*t*0.15);
}
void main()
{
 clrscr();
 int p, t, inter;
 cout<<"Enter the principle";
 cin>>p;
 cout<<"Enter the time period in year";
 cin>>t;
 inter=interest(p,t);
 cout<<"The interest is"<<inter;
 getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
/*WRITE A PROGRAM TO FIND AREA OF RECTANGLE USING INLINE FUNCTIONS*/

#include<iostream.h>
#include<conio.h>
inline int area(inta,intb)
{
 return (a*b);
}
void main()
{
 clrscr();
 int a, b, ar;
 cout<<"Enter the length";
 cin>>a;
 cout<<"Enter the breath";
 cin>>b;
 cout<<"The area is"<<ar;
 getch();
}

----------------------------------------------------------
OUTPUT:-


Share
Tweet
Pin
Share
1 comments
/*C++ PROGRAMMING EXAMPLES*/

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 cout<<"HELLO COMPILER, I AM C++";
 getch();
}

----------------------------------------------------------
OUTPUT:-

Share
Tweet
Pin
Share
No comments
Newer Posts

About me

recent posts

Sponsor

Facebook

Blog Archive

  • December 2018 (28)

Report Abuse

About Me

Unknown
View my complete profile

Created with by ThemeXpose | Copy Blogger Themes