0% found this document useful (0 votes)
12 views

C++ Record Output and Program

The document discusses several C++ programs that illustrate OOP concepts like constructors, destructors, classes, objects, inheritance, polymorphism and virtual functions. It includes the code and output for programs on constructors, destructors, arrays of objects, friend functions, inline functions, single inheritance and multiple inheritance.

Uploaded by

bala vinothini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C++ Record Output and Program

The document discusses several C++ programs that illustrate OOP concepts like constructors, destructors, classes, objects, inheritance, polymorphism and virtual functions. It includes the code and output for programs on constructors, destructors, arrays of objects, friend functions, inline functions, single inheritance and multiple inheritance.

Uploaded by

bala vinothini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Page

DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE CONSTRUCTORS AND DESTRUCTOR /*


#include<iostream.h>
class student
{
private:
char name[20],add[20];
int roll,zip;
public:
student ( );
~student( );
void read( );
void disp( );
};
student :: student()
{
cout<<"Student class constructor called."<<endl;
}
void student :: read()
{
cout<<"Enter the student Name: ";
cin>>name;
cout<<"Enter the student roll no:";
cin>>roll;
cout<<"Enter the student address: ";
cin>>add;
cout<<"Enter the Zipcode: ";
cin>>zip;
}
void student :: disp()
{
cout<<"Studet details"<<endl;
cout<<"Student Name:"<<name<<endl;
cout<<"Roll no is :"<<roll<<endl;
cout<<"Address is :"<<add<<endl;
cout<<"Zipcode is :"<<zip<<endl;
}
student :: ~student()
{
cout<<"Student class destructor called.";
}
int main()
{
clrscr();
student s;
s.read();
s.disp();
return 0;
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/Program to illustrate Array of objects/*

#include<iostream.h>
#include<conio.h>
class books
{
char title[30];
float price ;
public:
void getdata();
void putdata();
};
void books :: getdata()
{
cout<<"Title:";
cin>>title;
cout<<"Price:";
cin>>price;
}
void books :: putdata()
{
cout<<"Title:"<<title<< "\n";
cout<<"Price:"<<price<< "\n";
}
const int size=3;
int main()
{
clrscr();
books book[size];
for(int i=0;i<size;i++)
{
cout<<"Enter details of the book "<<(i+1)<<"\n";
book[i].getdata();
}
for(i=0;i<size;i++)
{
cout<<"\nBook "<<(i+1)<<"\n";
book[i].putdata();
}
return 0;
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE CLASS AND OBJECT/*

#include<iostream.h>
#include<conio.h>
class account
{
private:
int acc_no;
double balance;
static double rate;
public:
void read()
{
cout<<"\nEnter account number = ";
cin>>acc_no;
cout<<"\n Enter the Balance =";
cin>>balance;
}
void show()
{
cout<<"\nAccount no = "<<acc_no<< "\t";
cout<<"Interest = "<<rate<<"\t";
cout<<"Balance ="<<balance;
}
void qtr_rate_cal()
{
double interest=(balance*rate*0.25);
balance=balance+interest;
}
};
double account::rate=0.05;
int main()
{
clrscr();
account a1,a2;
cout<<"Enter customer 1 information \n";
a1.read();
cout<<"Enter customer 2 information \n";
a2.read();
a1.qtr_rate_cal();
a2.qtr_rate_cal();
a1.show();
a2.show();
getch();
return 0;
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE INLINE MEMBER FUNCTION/*

#include<iostream.h>
#include<conio.h>
inline int add(int x, int y)
{
return x+y;
}
inline int mul(int p, int q)
{
return p*q;
}
inline double div(int m , int n)
{
return m/n;
}
void main()
{
int x,y;
clrscr();
cout<<"Enter the Values :\n";
cin>>x>>y;
cout<<"Sum ="<<add(x,y)<<endl;
cout<<"Multiplication = "<<mul(x,y)<<endl;
cout<<"Division = "<<div(x,y)<<endl;
getch();
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE FRIEND FUNCTIONS*/

#include<iostream.h>
#include<conio.h>
class mark;
class info
{
private:
char name[30];
int reg;
public:
void getdata()
{
cout<<"Enter the name:\n";
cin>>name;
cout<<"Enter the register:\n";
cin>>reg;
}
friend void print(info f, mark k);
};
class mark
{
private:
int m1,m2,m3;
public:
void getdata()
{
cout<<"Enter the marks:\n";
cin>>m1>>m2>>m3;
}
friend void print(info f, mark k);
};
void print(info f, mark k)
{
cout<<"Name :"<<f.name<<endl;
cout<<"Register number :"<<f.reg<<endl;
cout<<"Mark 1 :"<<k.m1<<endl;
cout<<"Mark 2 :"<<k.m2<<endl;
cout<<"Mark 3 :"<<k.m3<<endl;
cout<<"Total Mark :"<<k.m1+k.m2+k.m3<<endl;
if(k.m1>=40 && k.m2>=40 && k.m3>=40)
cout<<"Result is pass"<<endl;
else
cout<<"Result is fail"<<endl;
}
void main()
{
info f1;

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

mark k1;
clrscr();
f1.getdata();
k1.getdata();
print(f1,k1);
getch();
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

/*PROGRAM TO ILLUSTRATE VIRTUAL FUNCTIONS*/


#include<iostream.h>
#include<conio.h>
class vaccine
{
public:
virtual void putVaccine()
{
cout<<"Covid Vaccine"<<endl;
}
};
class covaxin:public vaccine
{
public:
void putVaccine()
{
cout<<"Put Covaxin Vaccine"<<endl;
}
};
class covishield:public vaccine
{
public:
void putVaccine()
{
cout<<"Put Covishield Vaccine"<<endl;
}
};
int main()
{
clrscr();
covaxin cx;
covishield cs;
vaccine *o=NULL;
o=&cx;
o->putVaccine();
o=&cs;
o->putVaccine();
return 0;
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE SINGLE INHERITANCE*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class student
{
private:
char regno[9];
char name[25];
int age;
public:
void getdata();
void display();
};
void student::getdata()
{
cout<<"\n Enter the Register Number:";
cin>>regno;
cout<<"Enter the name:";
gets(name);
cout<<"Enter the age:";
cin>>age;
}
void student::display()
{
cout<<"\nStudent Personal Details";
cout<<"\n________________________";
cout<<"\n Register Number:"<<regno;
cout<<"\nName :"<<name;
cout<<"\nAge :"<<age;
}
class sports:public student
{
private:
char sportsname[25];
float score;
public:
void getdata()
{
student::getdata();
cout<<"\n Enter the sports name:";
gets(sportsname);
cout<<"Enter the sports score:";
cin>>score;
}
void display()

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

{
student::display();
cout<<"\n\nSports Details";
cout<<"\n\n______________";
cout<<"\n Sports NAme:"<<sportsname;
cout<<"\nSports Score:"<<score;
}
};
void main()
{
sports s1;
clrscr();
s1.getdata();
s1.display();
getch();
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PEOGRAM TO ILLUSTRATE MULTILPLE INHERITANCE*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class student
{
private:
int regno;
char name[25];
int age;
protected:
float boardexam;
public:
void getdata();
void display();
};
class entrance
{
protected:
float entper;
public:
void getdata();
void display();
};
class sports
{
protected:
float score;
public:
void getdata();
void display();
};
class result:public student,public entrance,public sports
{
private:
float totalscore;
public:
void getdata();
void calresult();
void display();
};
void student::getdata()
{cout<<"\n\tBase Class 1 -Student";
cout<<"\n\t=======================";
cout<<"\n Enter the Register Number:";
cin>>regno;

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

cout<<"\nEnter the Student Name:";


gets(name);
cout<<"Enter the Age";
cin>>age;
cout<<"\n Enter the Board Exam Percentage:";
cin>>boardexam;
}
void student:: display()
{
cout<<"\n\n Base Class1 Student";
cout<<"\n\n=====================";
cout<<"\n Register Number:"<<regno;
cout<<"\nStudent Name :"<<name;
cout<<"\nStudent Age :"<<age;
cout<<"\n Board Exam Percentage:"<<boardexam;
}
void entrance::getdata()
{
cout<<"\n\nBase Class2- Entrance";
cout<<"\n\n=====================";
cout<<"\n Enter the Entrance Exam Precentage:";
cin>>entper;
}

void entrance::display()
{
cout<<"\n\nBase Class2- Entrance";
cout<<"\n\n=====================";
cout<<"\n Entrance Exam Precentage:"<<entper;
}
void sports::getdata()
{
cout<<"\n\nBase Class3- Sports";
cout<<"\n\n=====================";
cout<<"\n EnterSports Score";
cin>>score;
}
void sports::display()
{
cout<<"\n\nBase Class3- Sports";
cout<<"\n\n=====================";
cout<<"\n Sports Score"<<score;
}
void result::getdata()
{
student::getdata();
entrance::getdata();
sports::getdata();

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

}
void result::calresult()
{
totalscore=boardexam+entper+score;
}
void result::display()
{

student::display();
entrance::display();
sports::display();
cout<<"\n\nDerived Class - Result";
cout<<"\n\n=====================";
cout<<"\n Total Score:"<<totalscore;
}
void main()
{
clrscr();
cout<<"\t\t******Multiple Inheritance***";
result s1;
s1.getdata();
s1.calresult();
s1.display();
getch();
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE OPERATOR OVERLOADING/*

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class complex
{
int real;
float image;
public:
void getdata()
{
cout<<"\nenter the real part of the complex::";
cin>>real;
cout<<"\nenter the imaginary part of the complex::";
cin>>image;
}
void operator+(complex);
void operator-(complex);
};
void complex::operator+(complex c1)
{
complex temp;
temp.real=real+c1.real;
temp.image=image+c1.image;
if (temp.image>=0)
{
cout<<"\ncomplex no. after addition::";
cout<<temp.real<<"+"<<temp.image<<"i\n";
}
else
{
cout<<"\ncomplex no. after addition::";
cout<<temp.real<<temp.image<<"i\n";
}
}
void complex::operator-(complex c1)
{
complex temp;
temp.real = real-c1.image;
temp.image=image-c1.image;
if (temp.image>=0)
{
cout<<"\n complex no. after subtraction :: ";
cout<<"\ntemp.real"<<"+"<<temp.image<<"i\n";
}
else

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

{
cout<<"\ncomplexno.aftersubtraction::";
cout<<temp.real<<temp.image<<"i\n";
}
}
int main()
{
clrscr();
complex c1,c2;
int n;
do
{
cout<<"\n1. Inputdata for complex no. ";
cout<<"\n 2. Addition of complex no. ";
cout<<"\n3.Subtraction of complexno.";
cout<<"\n 4. Quit";
cout<<"\nEnter yourchoice::";
cin>>n;
switch(n)
{
case 1:
cout<<endl<<"\nEnter the data for First ComplexNo";
c1.getdata();
cout<<endl<<"\nEnter the data for seconds Complex No";
c2.getdata();
break;
case 2:
c1+c2;
break;
case 3:
c1-c2;
break;
case 4:
break;
}
}while(n!=4);
return 0;
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

*/PROGRAM TO ILLUSTRATE FUNCTION OVERLOADING*/

#include<iostream.h>
#include<conio.h>
void area(int a)
{
int square=a*a;
cout<<"The Area Of Square Value is:"<<square<<endl;
}
void area(int l, int b)
{
int rectangle=l*b;
cout<<"The Area Of The Rectangle Value is:"<<rectangle<<endl;
}
void area(float r)
{
float circle=2.34*r*r;
cout<<"The Area of The Circle Value is:"<<circle<<endl;
}
void area(float le,float br)
{
float triangle=0.4*le*br;
cout<<"The Area Of the triangle value is:"<<triangle<<endl;
}

int main()
{
clrscr();
int a,b,l;
float r,le,br;
cout<<" "<<endl;
cout<<"AREA OF SHAPES USING FUNCTION OVERLOADING"<<endl;
cout<<" "<<endl;
cout<<"Enter one value for Square:";
cin>>a;
cout<<endl;
cout<<"Enter two value for Rectangle:";
cin>>b>>l;
cout<<endl;
cout<<"Enter one value for Circle:";
cin>>r;
cout<<endl;
cout<<"Enter two values for triangle:";
cin>>le>>br;
cout<<endl;
cout<<" "<<endl;
area(a);

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

area(l,b);
area(r);
area(le,br);
cout<<" "<<endl;
return 0;
}

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

OUTPUT:

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.


Page
DEPATMENT OF COMPUTER SCIENCE

PONDICHERRY UNIVERSITY COMMUNITY COLLEGE, LAWSPET, PUDUCHERRY.

You might also like