0% found this document useful (0 votes)
68 views42 pages

C++ RECORD dgvc-2

The document outlines a lab manual for Object Oriented Programming using C++ at Dwaraka Doss Goverdhan Doss Vaishnav College. It includes various programming exercises such as simple interest calculations, area and perimeter of a triangle, average of numbers, function and operator overloading, quadratic equations, single and multiple inheritance, virtual functions, and file operations. Each exercise provides the aim, program code, output, and results confirming successful implementation.

Uploaded by

jink73639
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views42 pages

C++ RECORD dgvc-2

The document outlines a lab manual for Object Oriented Programming using C++ at Dwaraka Doss Goverdhan Doss Vaishnav College. It includes various programming exercises such as simple interest calculations, area and perimeter of a triangle, average of numbers, function and operator overloading, quadratic equations, single and multiple inheritance, virtual functions, and file operations. Each exercise provides the aim, program code, output, and results confirming successful implementation.

Uploaded by

jink73639
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

DWARAKA DOSS GOVERDHAN DOSS VAISHNAV

COLLEGE (AUTONOMOUS)
ARUMBAKKAM, CHENNAI-600 106.

DEPARTMENT OF PHYSICS WITH COMPUTER


APPLICATIONS

OBJECT ORIENTED PROGRAMMING USING


C++ LAB
DWARAKA DOSS GOVERDHAN DOSS VAISHNAV COLLEGE
(AUTONOMOUS)
ARUMBAKKAM, CHENNAI-600 106.

Certified that this is a record work done by


……………………………………. whose Register no.
………………………… of II P.C.A. during the academic year
2022-2023.

Faculty In-charge Head of the Department

Submitted for practical examination held on ………….


…………… at Dwaraka Doss Goverdhan Doss Vaishnav
College, Chennai-106.
Internal Examiner External Examiner

TABLE OF CONTENTS

S.NO. DATE PROGRAM NAME PG.NO SIGNATURE

1 SIMPLE INEREST
28.07.22
CALCAULATIONS

2 AREA AND PERIMETER OF


18.08.22
TRIANGLE

3 27.08.22 AVERAGE OF N NUMBERS

4 06.09.22 FUNCTION OVERLOADING

5 12.09.22 OPERATOR OVERLOADING

6 19.09.22 QUADRATIC EQUATIONS

7 11.10.22 SINGLE INHERITANCE

8 18.10.22 VIRTUAL FUNCTIONS

9 COPYING THE CONTENT FROM


28.10.22
ONE FILE TO ANOTHER FILE
10 04.11.22 MULTIPLE INHERITANCE

SIMPLE INEREST CALCAULATIONS

Ex.no : 01

Date : 28.07.22

AIM:
To demonstrate the usage of Simple interest
Calculations in C++ program.

PROGRAM:

#include <iostream.h>
#include <conio.h>
int main( )
{
clrscr( );
float p,r,t,si;
cout<<"enter principle amount:";
cin>>p;
cout<<"enter rate of interest:";
cin>>r;
cout<<"enter time period:";
cin>>t;
si=(p+r+t)/100;
cout<<"simple interest amount:"<<si<<endl;
return 0;}
OUTPUT:

Enter Principle Amount: 1233


Enter Rate of Interest: 2
Enter Time Period: 1
Simple Interest Amount: 12.36

RESULT:
Thus, finding Simple Interest Calculation using C++
Program is implemented and verified successfully.

AREA AND PERIMETER OF TRIANGLE

Ex.no : 02
Date : 18.08.22

AIM:
To demonstrate the usage of Area and Perimeter of
Triangle in C++ program.

PROGRAM:

#include <iostream.h>
#include <math.h>
#include <conio.h>
class triangle
{
private:
float a,b,c,s,aa;
public:
void getdata();
float area();
float perimeter();
};
void triangle::getdata()
{
cout<<"enter the length of first sides:";
cin>>a;
cout<<"enter the length of second sides:";
cin>>b;
cout<<"enter the length of thrid sides:";
cin>>c;
}
float triangle::area()
{
s=(a+b+c)/2;
aa=sqrt(s*(s-a)*(s-b)*(s-c));
return aa;
}
float triangle::perimeter()
{
return(a+b+c);
}
void main()
{
clrscr();
triangle t;
float ar,pr;
t.getdata();
ar=t.area();
cout<<"area="<<ar<<endl;
pr=t.perimeter();
cout<<"perimeter="<<pr<<endl;
}
OUTPUT:

Enter The Length of First Sides: 12


Enter The Length of Second Sides: 12
Enter The Length of Third Sides: 12
Area: 62.353828
Perimeter: 36

RESULT:
Thus, finding Area And Perimeter of Triangle using
C++ Program is implemented and verified successfully.

AVERAGE OF N NUMBERS

Ex.no : 03
Date : 27.08.22

AIM:
To demonstrate the usage of Average of N numbers in
C++ program.

PROGRAM:

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int n,i;
float num[100],sum=0.0,avg;
cout<<"enter the number of data:";
cin>>n;
while(n>100||n<=0)
{
cout<<"error!number should in range of 1 to 100:"<<endl;
cout<<"enter the number again:";
cin>>n;
}
for(i=0;i<n;i++)
{
cout<<i+1<<"enter the number:";
cin>>num[i];
sum+=num[i];
}
avg=sum/n;
cout<<"average="<<avg;
return 0;
}
OUTPUT:

Enter The Numbers of Data: 4


1Enter The Number: 12
2Enter The Number:89
3Enter The Number:87
4Enter The Number:54
Average=60.5

RESULT:
Thus, finding Average of N Numbers using C++
Program is implemented and verified successfully.
FUNCTION OVERLOADING

Ex.no : 04
Date : 06.09.22

AIM:
To demonstrate the usage of Function Overloading in
C++ Program.

PROGRAM:

#include <iostream.h>
#include <conio.h>
void display(int var1,double var2)
{
cout<<"integer number:"<<var1;
cout<<" and double number:"<<var2;
}
void display(double var)
{
cout<<"double number:"<<var<<endl;
}
void display(int var)
{
cout<<"integer number:"<<var<<endl;
}
int main()
{
clrscr();
int a= 5;
double b= 5.5;
display(a);
display(b);
display(a,b);
return 0;
}
OUTPUT:

Integer Number: 5
Double Number: 5.5
Integer Number: 5 And Double Number: 5.5

RESULT:
Thus, finding Functoin Overloading using C++
Program is implemented and verified successfully.

OPERATOR OVERLOADING

Ex.no : 05
Date : 12.09.22

AIM:
To demonstrate the usage of Operator Overloading in
C++ program.

PROGRAM:

#include <iostream.h>
#include <conio.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<<"+j"<<y<<"\n";
}
int main()
{
clrscr();
complex C1,C2,C3;
C1=complex(2.5,3.5);
C2=complex(1.6,2.7);
C3=C1+C2;
cout<<"c1=";C1.display();
cout<<"c2=";C2.display();
cout<<"c3=";C3.display();
return 0;}
OUTPUT:

C1= 2.5+j3.5
C2= 1.6+j2.7
C3= 4.1+j6.2

RESULT:
Thus, finding Operator Overloading using C++
Program is implemented and verified successfully.

QUADRATIC EQUATIONS

Ex.no : 06
Date : 12.09.22
AIM:
To demonstrate the usage of Quadratic Equations in
C++ program.

PROGRAM:

#include<iostream.h>
#include<math.h>
#include<conio.h>
int main()
{
clrscr();
float a,b,c,x1,x2,d,real,imaginary;
cout<<"Quadratic Equation : ax^2 + bx + c";
cout<<"\nEnter coefficient of a : ";
cin>>a;
cout<<"Enter coefficient of b : ";
cin>>b;
cout<<"Enter coefficient of c : ";
cin>>c;
d=b*b-4*a*c;
if(d>0)
{
cout<<"Roots are real and distinct";
x1=(-b + sqrt(d)) / (2*a);
x2=(-b - sqrt(d)) / (2*a);
cout<<"\nx1 = "<<x1;
cout<<"\nx2 = "<<x2;
}
else if(d==0)
{
cout<<"Roots are real and same";
x1=(-b + sqrt(d)) / (2*a);
cout<<"\nx1 = "<<x1;
cout<<"\nx2 = "<<x1;
}
else
{
cout<<"Roots are real and distinct";
real = -b/(2*a);
imaginary = sqrt(-d) / (2*a);
cout<<"\nx1 = "<<real<<"+"<<imaginary<<"i";
cout<<"\nx2 = "<<real<<"-"<<imaginary<<"i";
}
return 0;
}
OUTPUT:

Quadratic equation : ax^2 + bx + c


Enter Coefficient of a : 1
Enter Coefficient of b : 2
Enter Coefficient of c : -8
Roots Are Real And Distinct
X1 = 2
X2 = -4

RESULT:

Thus, finding Quadratic Equations using C++ Program


is implemented and verified successfully.
SINGLE INHERITANCE

Ex.no : 07
Date : 11.10.22

AIM:
To demonstrate the usage of Single Inheritance in C+
+ program.

PROGRAM:

#include <iostream.h>
class student
{
private:
char name[20];
int age;
char gender;
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"enter the name:";
cin>>name;
cout<<"enter the age:";
cin>>age;
cout<<"enter the gender:";
cin>>gender;
}
void student::putdata()
{
cout<<"name:"<<name<<"age:"<<age<<"gender:"<<gende
r<<endl;
}
class result:public student
{
private:
int totalmarks;
float perc;
char grade;
public:
void getresultinfo();
void putresultinfo();
};
void result::getresultinfo()
{
cout<<"enter the totalmarks:";
cin>>totalmarks;
perc=float(totalmarks*100/500);
if (perc>=80)
{
cout<<"distinstion";
}
else if (perc>=60)
{
cout<<"first class";
}
else if (perc>=50)
{
cout<<"second class";
}
else if (perc>=40)
{
cout<<"third class";
}
else;
{
cout<<"fail";
}
void result::putresultinfo()
{
cout<<"marks:"<<totalmarks<<"percentage:"<<perc<<"gra
de:"<<grade<<endl;
}
int main:
{
result r;
r.getdata();
r.putdata();
r.getresultinfo();
r.putresultinfo();
return 0;}
OUTPUT:

Enter The Name: Arun


Enter The Age: 19
Enter The Gender: Male
Name: Arun
Age: 19
Gender: M
Enter The Totalmarks: 400
Marks:400
Percentage: 80
Grade: Distinstion

RESULT:

Thus, finding Single Inheritance using C++ Program


is implemented and verified successfully.
VIRTUAL FUNCTIONS

Ex.no : 08

Date : 18.10.22

AIM:
To demonstrate the usage of Virtual Function in
C++ program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void print()
{
cout<<"print base class:"<<endl;
}
void show()
{
cout<<"show base class:"<<endl;
}
};
class derived:public base
{
public:
void print()
{
cout<<"print derived class:"<<endl;
}
void show()
{
cout<<"show derived class"<<endl;
}
};
int main()
{
clrscr();
base *bptr;
derived d;
bptr= &d;
bptr->print();
bptr->show();
return 0;
}
OUTPUT:

Print Derived Class:


Show Base Class:

RESULT:

Thus, finding Virtual Functions using C++ Program is


implemented and verified successfully.
COPYING THE CONTENT FROM ONE FILE TO
ANOTHER FILE
Ex.no : 9
Date : 28.10.22

AIM:
To demonstrate the usage of Copying the content from
one file to another file in C++ program.

PROGRAM:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream fs;
ofstream ft;
string str;
char sourcefile[50], destinationfile[50];
cout << "Enter Source File with Extension: ";

gets(sourcefile);

fs.open(sourcefile);

if (!fs)
{
cout << "Error in Opening Source File...!!!";
exit(1);
}

cout << "Enter Destination File with Extension: ";

gets(destinationfile);

ft.open(destinationfile);

if (!ft)
{
cout << "Error in Opening Destination File...!!!";
fs.close();
exit(2);
}

if (fs && ft)


{
while (getline(fs, str))
{
ft << str << "\n";
}

cout << "\n\n Source File Date Successfully


Copied to Destination File...!!!";

}
else
{
cout << "File Cannot Open...!!!";
}
cout << "\n\n Open Destination File and Check!!!\n";

fs.close();
ft.close();
}
OUTPUT :

Enter Source File With Extension:


File1.txt
Enter Destination File with Extension:
File 2.txt
Source File Date Successfully Copied to Destination File

RESULT:

Thus, finding Copying the content from one file to


another file using C++ Program is implemented and
verified successfully.
MULTIPLE INHERITANCE

Ex.no : 10
Date : 04.11.22

AIM:
To demonstrate the usage of Multiple Inheritance in
C++ program.

PROGRAM:

#include <iostream.h>
#include <conio.h>
class student
{
protected:
int rno,sum,i,marks[5];
public:
void detail()
{
cout<<"enter the roll no.:"<<endl;
cin>>rno;
cout<<"enter the marks of 5 subjects:"<<endl;
for (i=0;i<5;i++)
{
cin>>marks[i];
}
sum=sum+marks[i];
}
};
class sports
{
protected:
int s_mark;
public:
void get_mark()
{
cout<<"enter the sports mark:"<<endl;
cin>>s_mark;
}
};
class result:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=sum+s_mark;
avg=tot/6;
cout<<"\n\n\t rollno.:"<<rno<<"\n\t total:"<<tot<<endl;
cout<<"\n\t average marks:"<<avg;
}
};
int main()
{
clrscr();
result r;
r.detail();
r.get_mark();
r.display();
return 0;
}
OUTPUT:

Enter the roll no.:


26
Enter The Marks of 5 Subjects:
92
94
98
90
95
Enter The Sports mark:
99
Rollno.:26
Total : 1505
Average marks : 250

RESULT:

Thus, finding Multiple Iheritance using C++ Program


is implemented and verified successfully.

You might also like