Académique Documents
Professionnel Documents
Culture Documents
#include <iostream>
using namespace std;
class complex
{
public :
int real, img;
};
int main()
{
complex a, b, c;
cout << "Enter a and b where a + ib is the first complex number.";
cout << "\na = ";
cin >> a.real;
cout << "b = ";
cin >> a.img;
cout << "Enter c and d where c + id is the second complex number.";
cout << "\nc = ";
cin >> b.real;
cout << "d = ";
cin >> b.img;
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i";
else
cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";
return 0;
}
return 0;
}
float sum;
sum = x + y;
return sum;
}
Output of program:
C programming
C programming
C++ programming
// global variable
int main() {
char c = 'b'; //local variable
cout << "Local c: " << c << "\n";
cout << "Global c: " << ::c << "\n"; //using scope resolution operator
return 0;
}
return 0;
}
Write a Program to calculate the area of a circle using class & Object
#include<iostream.h>
#include<conio.h>
class raji
{
private:
int radius;
float area;
public:
void getdata()
{
cout<<"Enter the radius"<<endl;
cin>>radius;
}
void calculate()
{
area=3.14*radius*radius;
}
void showdata()
{
cout<<"The area of the circle is "<<area<<endl;
}
};
void main()
{
raji r;
r.getdata();
r.calculate();
r.showdata();
getch();
}
ARRAY OF OBJECTS
#include<iostream.h>
#include<conio.h>
class circle {
private:
int area, radius;
public:
void getdata() {
cout<<"Enter the radius value"<<endl;
cin>>radius;
}
void calculate() {
area=3.14*radius*radius;
}
void show() {
cout<<"the area is "<<area<<endl;
}
};
void main() {
int i;
circle e[5];
for(i=0;i<5;i++)
{
e[i].getdata();
e[i].calculate();
e[i].show(); }
getch(); }
Output
Enter the radius value 1
The area is 3
Enter the radius value 2
The area is 12
Enter the radius value 3
The area is 28
Enter the radius value 4
The area is 50
Enter the radius value 4
The area is 50
FRIEND FUNCTION
#include<iostream.h>
class book
{
private :
int bno;
char bname[20];
public:
void getdata();
friend void show(book);
};
void book::getdata()
{
cin>>bno>>bname;
}
void show(book bk)
{
cout<<bk.bno<<bk.bname;
}
void main()
{
book b;
clrscr();
b.getdata();
show(b);
getch();
}
Friend Function -2
#include<iostream.h>
class second;
class first
{
private :
int no;
public:
first(int n);
friend int add(first,second);
};
class second
{
private :
int n1;
public:
second(int);
friend int add(first,second);
};
first ::first(int n)
{
no=n;
}
second ::second(int n)
{
n1=n;
}
int add(first f,second s)
{
cout<<"First class="<<f.no<<endl;
cout<<"Second class= <<s.n1;
return f.no+s.n1;
}
void main()
{
first f(10);
second s(20);
clrscr();
cout<<"\n The Result is "<<add(f,s);
}
FRIEND CLASS
#include<iostream.h>
#include<conio.h>
class second;
class first
{
private :
int no;
public:
friend class second;
first(int n)
{
no=n;
}};
class second
{
public:
void show(first f)
{
cout<<f.no;
}
};
void main()
{
first f(10);
second s;
s.show(f);
getch();
}
DEFAULT CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
class exam {
private:
int sno,mark1,mark2;
public:
exam( ) {
sno = mark1 = mark2 =100;
}
void showdata()
{
cout<<"Sno"<<sno <<"Mark1 <<mark1<<"Mark2<<mark2 ;
}
exam(int a,int m1,int m2)
{
sno=a;
mark1=m1;
mark2=m2;
}};
void main()
{
exam e,e1;
clrscr();
e.showdata();
e.getdata();
e.showdata();
getch();
}
Overloaded Constructor
#include<iostream.h>
class circle
{
private:
int radius;
float area;
int b,h;
public:
circle(){
cout<<"Enter the radius "<<endl;
cin>>radius;
area=3.14 * radius * radius;
cout<<"area of the circle"<< area;
}
};
circle(int r){
radius=r;
area=3.14 * radius * radius;
cout<< "\narea of the circle is " << area; }
circle(int x,int y){
b=x;
h=y;
area=0.5*b*h;
cout<< "\n area of the Triangle is " << area;}
void main()
{
circle c;
circle d1(6);
circle c1(3,4);
}
void main()
{
clrscr();
hdca h;
cout<<endl<<"Fees detail for PCP"<<endl;
h.pcp_getfees();
cout<<endl<<"Fees detail for HDCA"<<endl;
h.hdca_getfees();
cout<<endl<<"Fees list for PCP"<<endl;
h.pcp_listfees();
cout<<endl<<"Fees list for HDCA"<<endl;
h.hdca_listfees();
getch();
}
MULTIPLE INHERITANCE
#include<iostream.h>
class base1
{
protected :
int var1;
public :
void disp_base1() {
cout<<"var1 "<<var1<<endl;
}};
class base2
{
protected :
int var2;
public :
void disp_base2()
{
cout<<"var2 is"<<var2<<endl;
}};
class deri:public base1,public base2
{
private:
int var3;
public :
{
employee::getdata();
cout<<"Enter Basic Salary \n";
cin>>basic;
}
void putdata()
{
da=0.30*basic;
hra=0.15*basic;
cca=0.05*basic;
pf=0.12*basic;
special_allowances=0.4*basic;
salary=basic+da+hra+cca+special_allowances-pf;
employee::putdata();
cout<<endl<<salary;
}
};
class worker :public employee
{
public :
float salary;
float wages;
float incentive;
void getdata()
{
employee::getdata();
cout<<"\n Enter wages & incentives";
cin>>wages>>incentive;
}
void putdata()
{
salary=wages+incentive;
employee::putdata();
cout<<endl<<salary;
}
};
void main()
{
manager m;
supervisor s;
worker w;
clrscr();
m.getdata();
s.getdata();
w.getdata();
w.putdata();
s.putdata();
m.putdata();
getch();
}
HYBRID INHERITANCE
#include<iostream.h>
class lecturer
{
private :
char lecturer_name[20];
public :
void getdata()
{
cout<<"Enter Lecturer name";
cin>>lecturer_name;
}
void putdata()
{
cout<<"lecturer name is "<<lecturer_name;
}
};
class department : public lecturer
{
private :
char department_name[20];
public:
void getdata()
{
cout<<"Enter Department name";
cin>>department_name;
}
void putdata()
{
void main()
{
student s;
clrscr();
s.getdata();
s.putdata();
getch();
}