//Program to display add,sub,mult,divi by member function

| |


#include<iostream.h>
#include<conio.h>
class sample{
private:
int x;
int y;
public:
void getinfo();
void display();
int sum();
int diff();
int mult();
int div();
};
void sample::getinfo()
{
cout<<"enter any two no?"<<endl;
cin>>x>>y;
}
void sample::display()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"sum="<<sum()<<endl;
cout<<"diff="<<diff()<<endl;
cout<<"mult="<<mult()<<endl;
cout<<"div="<<div()<<endl;
}
int sample::sum()
{
return(x+y);
}
int sample::diff()
{
return(x-y);
}
int sample::mult()
{
return(x*y);
}
int sample::div()
{
return(x/y);
}
void main(void)
{
sample obj1;
obj1.getinfo();
obj1.display();
obj1.sum();
obj1.diff();
obj1.mult();
obj1.div();
}
Read More

//Program to show content of dateon screen

| |



#include<iostream.h>
#include<conio.h>
 void main(void)
 {
 class date{
 public:
 int day;
 int month;
 int year;
 };
 class date today;
 today.day=10;
 today.month=3;
 today.year=1995;
 cout<<"today's date is="<<today.day<<"/";
 cout<<today.month<<"/"<<today.year<<endl;
 }
Read More

/* Program to add to complex number using constructor and destructor*/

| |


#include<iostream.h>
#include<conio.h>
class complex
{
float real,img;
public:
void getdata()
{
cout<<"\nEnter the real number->";
cin>>real;
cout<<"\nEnter the imaginary number->";
cin>>img;
}
void show_add_data()
{
cout<<"\nThe addition of complex numbers is->"<<real<<"+"<<img<<"i\n";
}
void show_mul_data()
{
cout<<"\nThe multiplication of complex number is->"<<real<<"*"<<img<<"i\n";
}
complex operator+(complex c)
{
complex t;
t.real=real+c.real;
t.img=img+c.img;
return t;
}
complex()
{
cout<<"Constucting\n";
}
~complex()
{
cout<<"Destructing\n";
}
};
void main()
{
complex c1,c2,c3,c4;
c1.getdata();
c2.getdata();
c3=c1+c2;
c3.show_add_data();
c4.show_mul_data();
getch();
}
Read More

/* Program to add two distances, one in 'feet,inches' and other in 'meter and centimeter' and display the result in fact,inches*/

| |


#include<iostream.h>
#include<conio.h>
class mtrcm;
class dist
{
int feet; float inch;
public:
dist()
{
feet=inch=0;
}
dist(int f,float in)
{
feet=f;inch=0;
}
void show()
{
cout<<feet<<inch;
}
friend void add(dist,mtrcm);
};
class mtrcm
{
int mtr,cm;
public:
mtrcm(int m,int c)
{
mtr=m;cm=c;
}
friend void add(dist,mtrcm);
};
void add(dist dd, mtrcm mm)
{
float distfeet=dd.feet+dd.inch/12;
float distmtrcm=(mm.mtr+mm.cm/100)*3.28;
float distaddfeet=distfeet+distmtrcm;
dist res;
res.feet=int(distaddfeet);
res.inch=(distaddfeet-res.feet)*12.00;
cout<<"\nfeet="<<res.feet<<"\ninches="<<res.inch;
}
int main()
{
dist d1(2,0);
mtrcm m1(1,100);
add(d1,m1);
return 0;
}
Read More

//Program demonstrating creation and destruction of automatic, static and global objects.

| |


#include<iostream.h>
#include<conio.h>
#include<string.h>
class typeofobject
{
char msg[30];
public:
typeofobject(char s[1])
{
strcpy(msg,s);
cout<<"Object created: "<<msg<<"\n";
}
~typeofobject()
{
cout<<"Object destroyed: "<<msg<<"\n";
}
};
int main()
{  typeofobject t1("Global");
typeofobject t2("Automatic");
static typeofobject t3("Static");
return 0;
}
Read More