You are on page 1of 27

CP09 III SEM

OOPS WITH C++ LAB MANUAL


1. Write a C++ program to add two numbers using class concepts

Program:
#include<iostream>
using namespace std;
class numbers{
private:
int a;
int b;
public:
void readnumbers(void);
void printnumbers(void);
int addition(void);
};
void numbers :: readnumbers(void)
{
cout<<"Enter a value:";
cin>>a;
cout<<"Enter b value:";
cin>>b;
}
void numbers :: printnumbers(void)
{
cout<<"a="<<a<<"\nb="<<b<<endl;
}
int numbers :: addition(void)
{
return (a + b);
}

int main(){
numbers num;
int add;
num.readnumbers();
add = num.addition();
num.printnumbers();
cout<<"Addition = "<<add<<endl;
return 0;
}
OUTPUT:
Enter a value:5
Enter b value:6
a=5
b=6
Addition = 11

2. Write a C++ program to swap two numbers using class concepts


PROGRAM:
#include<iostream>
using namespace std;
class Swapp
{
int x,y;
public:
void getdata();
void Swappv();
void display();
};
void Swapp::getdata()
{
cout<<"Enter two numbers:";
cin>>x>>y;
}
void Swapp::Swappv()
{
x=x+y;
y=x-y;
x=x-y;
}
void Swapp::display()
{
cout<<"\n x="<<x<<"\n y="<<y;
}
int main()
{
Swapp S;
S.getdata();
cout<<"\n Before Swapp: \n";
S.display();
S.Swappv();
cout<<"\n After Swapp: \n";
S.display();
return 0;
}
OUTPUT:
Enter two numbers:55 73
Before Swapp:
x=55
y=73
After Swapp:
x=73
y=55

3. Write a C++ program to display student details by using class concepts


PROGRAM:
#include<iostream>
using namespace std;
class student
{
private:
char name[20],tkno[10],branch[10];
int sem;
public:
void input();
void display();
};
void student::input()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Tkno.:";
cin>>tkno;
cout<<"Enter Branch:";
cin>>branch;
cout<<"Enter Sem:";
cin>>sem;
}
void student::display()
{
cout<<"\nName:"<<name;
cout<<"\nTkno.:"<<tkno;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
}
int main()
{
student s;
s.input();
s.display();
}
OUTPUT:
Enter Name: SAI
Enter Tkno.:05
Enter Branch: IT
Enter Sem:3

Name: SAI
Tkno.:05
Branch: IT
Sem:3

4. Create an overloaded function in C++to convert int to float and to convert float to int.
PROGRAM:

5. Write a C++ program to perform overloading of binary operators


PROGRAM:
#include<iostream>
using namespace std;
class test
{
int a, b;
public:
test()
{
a=0;
b=0;
}
test(int x, int y)
{
a=x;
b=y;
}
test operator+(test t);
void display();
};
test test :: operator+(test t)
{
test temp;
temp.a= a+ t.a;
temp.b= b+ t.b;
return temp;
}
void test :: display()
{
cout<<"\n value of a\t"<<a;
cout<<"\n value of b\t"<<b;
}
int main()
{
test t1, t2, t3;
t1=test(10,20);
t2=test(30,40);
t3=t1+t2;
t1.display();
t2.display();
t3.display();
return 0;
}
OUTPUT:
value of a 10
value of b 20
value of a 30
value of b 40
value of a 40
value of b 60

6. Write a C++program to create a class called as complex, and implements the


following by overloading the function ADD that returns the COMPLEX number
a) ADD (s1, s2)-where s1is an integer (real part) and s2 is a COMPLEX number.
b) ADD (s1, s2)- where s1&s2 are complex numbers
c) Display the objects by using display function.
PROGRAM:
#include <iostream>
using namespace std;
class complex
{
int real, img;
public:
void get(int r, int i)
{
real = r;
img = i;
}
void show();
complex add(int, complex);
complex add(complex, complex);
};
complex complex :: add(int r, complex s1)
{
complex temp;
temp.real=r+s1.real;
temp.img = s1.img;
return temp;
}
complex complex :: add(complex s1, complex s2)
{
complex temp;
temp.real=s1.real+s2.real;
temp.img=s1.img+s2.img;
}
void complex :: show()
{
cout<<"\n"<<real<<"+"<<img<<"i";
}
int main()
{
complex s1, s2, s3, s4;
s1.get(5,3);
s2.get(6,4);
s1.show();
s2.show();
s3=s1.add(6,s1);
s4=s1.add(s1, s2);
s3.show();
s4.show();
return 0;
}
OUTPUT:
5+3i
6+4i
11+3i
7+0i

7. Write a C++program to create a class STRING and implement the following. Display
the results by overloading the operator << after every operation
a) STRING s1=”NTTF-NEC”
b) STRING s2 = ” BANGALORE”
c) STRING s3 = s1 +s2;
PROGRAM:
#include<iostream>
#include<string.h>
using namespace std;
class String
{
public:
char str[20];
public:
void accept_string()
{
cout<<"\n Enter String : ";
cin>>str;
}
void display_string()
{
cout<<str;
}
String operator+(String x) //Concatenating String
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
};
int main()
{
String str1, str2, str3.

str1.accept_string();
str2.accept_string();
str3=str1+str2;
cout<<"\n\n Concatenated String is : ";
str3.display_string();
return 0;
}
OUTPUT
Enter String : NTTF
Enter String : -NEC
Concatenated String is : NTTF-NEC

8. Write a C++ program to create a class called MATRIX using twodimensional array of
integers. Implement the following by overloading the operator==,which checks the
compatibility of two Matrices to be added and subtracted, perform the following by
overloading + and –operators. The program should read the matrix using >> operator
and display the results by overloading the operator<<
if(m1==m2)
{
m3 = m1+m2;
m4 = m1-m2;
}
else
Display appropriate error message.
PROGRAM:

9. Write a C++program to overload pre-increment, decrement and post increment,


decrement operators
PROGRAM:
PRE-INCREMENT
#include<iostream>
using namespace std;
class Integer{
private:
int i;
public:
//parameterised constructor
Integer(int i=0)
{
this->i=i;
}

//Overloading the prefix operator


Integer operator++()
{
Integer temp;
temp.i = ++i;
return temp;
}

//Function to display the value of i


void display()
{
cout<<"i="<<i<<endl;
}
};
//Driver function
int main()
{
Integer i1(3);

cout<<"Before increment:";
i1.display();

//using the pre-increment operator


Integer i2 = ++i1;

cout<<"After pre increment:";


i2.display();
}
OUTPUT:
Before increment:i=3
After pre increment: i=4

PRE-DECREMENT
PROGRAM:
#include<iostream>
using namespace std;
class Integer{
private:
int i;
public:
//parameterised constructor
Integer(int i=0)
{
this->i=i;
}

//Overloading the prefix operator


Integer operator--()
{
Integer temp;
temp.i = --i;
return temp;
}

//Function to display the value of i


void display()
{
cout<<"i="<<i<<endl;
}
};

//Driver function
int main()
{
Integer i1(3);
cout<<"Before decrement:";
i1.display();
//using the pre-decrement operator
Integer i2 = --i1;
cout<<"After pre decrement:";
i2.display();
}
OUTPUT:
Before decrement:i=3
After pre decrement:i=2

POST-INCREMENT
PROGRAM:
#include <iostream>
using namespace std;
class Test{
private:
int count;
public:
Test():count(5)
{

}
void operator ++()
{
count = count+1;
}
void Display()
{
cout<<"Count:"<<count;
}
};
int main(){
Test t;
++t;
t.Display();
return 0;
}
OUTPUT:
Count:6

POST-DECREMENT
PROGRAM:
#include<iostream>
using namespace std;
class Distance {
public:
//Member Object
int feet, inch;
//Constructor to initialize the object's value
Distance(int f, int i)
{
feet = f;
inch = i;
}
//Overloading(--)operator to perform decrement
void operator--()
{
feet--;
inch--;
cout<<"\nFeet & Inches(Decrement):"<<feet<<"''"<<inch;
}
};
int main()
{
//Declare and Initialize the constructor
Distance d1(8,9);
//Use(--) unary operator by single operand
--d1;
return 0;
}
OUTPUT:
Feet & Inches(Decrement):7''8

10. Write a C++ program to create a class called DATE; accept two valid dates inform of
dd/mm/yyyy. Implement the following by overloading operators – and +.Display the
results by overloading the operator << after every operation
a) No of days=d1-d2–whered1andd2aredateobjects; d1>=d2;and no _of _days is
an integer
b) d1 =d1+no_of_days where d1 is a date object and no_of _days is a integer
PROGRAM:
#include<iostream>
using namespace std;
class date
{
int dd,mm,yy;
int a[13];
long double dateno;
void getno();
public:
date()
{
a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;
a[4]=a[6]=a[9]=a[11]=30;
a[2]=28;
dd=mm=yy=1;
}
void getdate();
friend ostream& operator<<(ostream&,date&);
long double operator-(date&);
date operator+(long double);
};
void date::getdate()
{
cout<<"\n Enter date";
cout<<"\n\t day(dd):";
cin>>dd;
cout<<"\n\t month(mm):";
cin>>mm;
cout<<"\n\t year(yy):";
cin>>yy;
while((yy%4!=0&&dd>a[mm])||(yy%4==0 && mm==2 && dd>29) ||dd<=0 ||
mm>12 || mm<=0 )
{
cout<<"\n Invalid entry";
getdate();
}
getno();
}
void date::getno()
{
int m=1;
dateno=(long double)yy*365+yy/4;
if(yy%4>0)
dateno++;
while(m!=mm)
{
dateno+=a[m];
if(yy%4==0 && m==2)
dateno++;
m++;
}
dateno+=dd;
}
ostream& operator<<(ostream &out,date &d1)
{
out<<d1.dd<<"/"<<d1.mm<<"/"<<d1.yy;
return out;
}
long double date::operator-(date &b)
{
return(dateno-b.dateno);
}
date date::operator+(long double b)
{
for(int i=1;i<=b;i++)
{
dd++;
if(dd>a[mm])
{
mm++;
dd=1;
}
if(mm>12)
{
yy++;
mm=1;
}
if(yy%4==0)
a[2]=29;
}
return *this;
}
int main()
{
date d1,d2,d3;
d1.getdate();
cout<<"\n\td1="<<d1;
d2.getdate();
cout<<"\n\td2="<<d2;
long double diff, days;
diff=d1-d2;
cout<<"\n\n The difference between the two dates = ";
cout<<diff<<endl;
cout<<"\n\n Enter the no. of days to be added to the date "<<d1<<" :";
cin>>days;
d3=d1+days;
cout<<"\n New date is..."<<d3<<endl;
return 0;
}
OUTPUT:
Enter date
day(dd):12

month(mm):1

year(yy):89

d1=12/1/89
Enter date
day(dd):13

month(mm):1

year(yy):89

d2=13/1/89

The difference between the two dates = -1


Enter the no. of days to be added to the date 12/1/89 :20
New date is...1/2/89

11. Write a C++program to create a base class called student (name, regno, gender, and
branch) and using inheritance create a derived class exam with fields (subject, marks,
and semester) enter at least 4 students. Find total & average marks for each
semester separately and the average of all semesters
PROGRAM

#include <conio.h>
#include<bits/stdc++.h>
using namespace std;
//base class
class Student
{
private:
char name[30],gender[6];
int regno;
public:
//member function to get students detailss
void getDetails();
};

//sub class inheriting from base class(Student)


class Exam : public Student
{
public:
int sem, sub1, sub2, sub3;
double avg, tot;
public:
void accept_data()
{
cout<<"enter a sem from 1-6:";
cin>>sem;
if(sem==1||sem==2||sem==3||sem==4||
sem==5||sem==6)
{

cout<<"enter the marks for subj1:";


cin>>sub1;
cout<<"enter the subject for subj2:";
cin>>sub2;
cout<<"enter the subject for subj3:";
cin>>sub3;
}
else
{
cout<<"incorrect input";
}
}
void display_data()
{
cout<<"\nMarks of sub1:"<<sub1;
cout<<"\nMarks of sub2:"<<sub2;
cout<<"\nMarks of sub3:"<<sub3;
}
};
//class result derived from class exam
class result:public Exam
{
public:
void calculate()
{
tot=sub1+sub2+sub3;
cout<<"\ntotal marks:"<<tot;
avg=(sub1+sub2+sub3)/3;
cout<<"\naverage marks of the sem is:"<<avg;
}

};
//member function definition ,outside of the class
void Student::getDetails()
{
cout<<"\nenter name:";
cin>>name;
cout<<"enter gender:";
cin>>gender;
cout<<"enter regno:";
cin>>regno;
}

//main function
int main()
{
Student st;
Exam xt;
result rt;
int i,n,m;
float average;
cout<<"\nenter the no. of student entries:";
cin>>n;

for(i=0;i<n;i++)
{
rt.getDetails();
rt.accept_data();
rt.display_data();
rt.calculate();
}
cout<<endl;
getch();
}
OUTPUT:
enter the no. of student entries:4
enter name: ANIL
enter gender: MALE
enter regno:1
enter a Sem from 1-6:2
enter the marks for subj1:78
enter the subject for subj2:89
enter the subject for subj3:90
Marks of sub1:78
Marks of sub2:89
Marks of sub3:90
total marks:257
average marks of the sem is:85
enter name: BALU
enter gender: MALE
enter regno:2
enter a sem from 1-6:4
enter the marks for subj1:68
enter the subject for subj2:89
enter the subject for subj3:97
Marks of sub1:68
Marks of sub2:89
Marks of sub3:97
total marks:254
average marks of the sem is:84
enter name: SRAVS
enter gender: FEMALE
enter regno:3
enter a sem from 1-6:5
enter the marks for subj1:96
enter the subject for subj2:95
enter the subject for subj3:78
Marks of sub1:96
Marks of sub2:95
Marks of sub3:78
total marks:269
average marks of the sem is: 89
enter name: SARAH
enter gender: FEMALE
enter regno:4
enter a sem from 1-6:1
enter the marks for subj1:78
enter the subject for subj2:86
enter the subject for subj3:85
Marks of sub1:78
Marks of sub2:86
Marks of sub3:85
total marks:249
average marks of the sem is: 83
12.Create a class EMPLOYEE contains members (emp_id, emp_name, basic salary, net
salary). Create a derived class INCOME contains the members (DA, HRA). Another
derived class DEDUCTION consists of (IT –Income Tax, LIC). Use appropriate functions
to read data and calculate the net salary and print the complete data of multiple
employees. (DA = 50% of the basic, HRA=25%ofBasic, IT=30%ofthe Gross salary,
LIC=1800)
PROGRAM:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

class worker
{
protected:
int code;
char name[200];
float salary;
public:
worker()
{
}
worker(int c, char *n, float s)
{
code=c;
strcpy(name,n);
salary=s;
}
void putw()
{
cout<<"\n Code : "<<code;
cout<<"\n Name : "<<name;
cout<<"\n Salary : "<<salary;
}
};
class officer
{
protected:
float DA, HRA;
public:
officer()
{
}
officer(float d, float h)
{
DA=d;
HRA=h;
}
void puto()
{
cout<<"\n DA : "<<DA;
cout<<"\n HRA : "<<HRA;
}
};
class manager: public worker, public officer
{
private:
float TA;
float gsal;
public:
manager ()
{
}
manager (int c, char *n, float s, float d, float h): worker (c, n, s), officer (d, h)
{
}
void putm ()
{
putw ();
puto ();
TA=0.10*salary;
cout<<"\n TA : "<<TA;
gsal=DA+HRA+TA+salary;
cout<<"\n Gross Salary: "<<gsal;
}
};
int main()
{

manager m;
cout<<"\n Enter Worker Information for \n";

cout<<"\n Enter Code : ";


int c;
cin>>c;
cout<<"\n Enter Name : ";
char n[200];
cin>>n;
cout<<"\n Enter Salary : ";
float s;
cin>>s;
cout<<"\n Enter DA : ";
float d;
cin>>d;
cout<<"\n Enter HRA : ";
float h;
cin>>h;
m=manager (c, n, s, d, h);

cout<<"\n Manager Information ";


cout<<"\n-------------------------------";
m.putm ();

return 0;
}

OUTPUT:
Enter Worker Information for

Enter Code : 12

Enter Name: KOMAL

Enter Salary: 20000

Enter DA: 1500

Enter HRA: 2000

Manager Information
-------------------------------
Code: 12
Name: KOMAL
Salary: 20000
DA: 1500
HRA: 2000
TA: 2000
Gross Salary: 25500
EXP 13. Create a base class Student (roll _no, name, sem) from which derive two derived
class Test (part A, partB marks) and Sports (sports mark).Create one more derived class
Result(total)which has to inherit the properties of Test, Sports. Create appropriate
functions in each class to read the data, calculate the total and display complete details in
derived class by using Virtual Base class concept.
PROGRAM:
#include<iostream>
using namespace std;
class student {
protected:
char tno [15];
int m1, m2, m3;
public:
void get () {
cout << "Enter the Roll no:";
cin>>tno;
cout << "Enter the first subject marks :";
cin >> m1;
cout << "Enter the second subject marks :";
cin >> m2;
cout << "Enter the third subject marks :";
cin >> m3;
}
};

class sports {
protected:
int sm;
public:

void getsm () {
cout << "\nEnter the sports mark:";
cin>>sm;

}
};

class result : public student, public sports {


int tot, avg;
public:
void display () {
tot = (m1 + m2 + m3+ sm);
avg = tot / 4;
cout<<"\n\n\ttotal theory marks:"<<m1+m2+m3;
cout << "\n\n\tRoll No: " << tno << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};

int main () {
result obj;
obj.get();
obj. getsm ();
obj. display ();
return 0;
}
OUTPUT:
Enter the Roll no :01
Enter the first subject marks :20
Enter the second subject marks :30
Enter the third subject marks :40

Enter the sports mark :25


total theory marks :90
Roll No: 01
Total: 115
Average: 28

EXP 14. Create a base class called Shape. Use this class to store two double type values
that could be used to compute the area of figures. Derive two specific classes called
triangle and rectangle from the base shape. Add to the base class, a member function
get_ data () to initialize base class data members and another member function display
area () to compute and display the area of figures, make display area () as a virtual
function, and redefine this function in the derived classes to suit their requirements
PROGRAM:
#include <iostream>
using namespace std;
class Shape {
public: double a, b;
void get_data ()
{
cin>>a>>b;
}
virtual void display_area () = 0;
};
class Triangle: public Shape
{
public: void display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
};
class Rectangle: public Shape
{
public: void display_area ()
{
cout<<"Area of rectangle "<<a*b<<endl;
}
};
int main ()
{
Triangle t;
Shape *st = &t;
cout<<"Enter base and altitude: ";
st->get_data();
st->display_area ();
Rectangle r;
Shape *sr = &r;
cout<<"Enter length and breadth: ";
sr->get_data();
sr->display_area();
return 0;
}
OUTPUT:
Enter base and altitude: 10 5
Area of triangle 25
Enter length and breadth: 10 20
Area of rectangle 200
17. Write a C++program to demonstrate the Exception handling for Division by zero
exception and Array Index out of bounds exception.
PROGRAM:
#include <iostream>
#include <stdexcept>
using namespace std;
float Division (float num, float den) {
if (den == 0) {
throw runtime_error ("Math error: Attempted to divide by Zero\n");
return (num / den);
}
int main () {
float numerator, denominator, result;
cout<<"enter the numerator";
cin>>numerator;
cout<<"enter the denominator";
cin>>denominator;
try {
result = Division (numerator, denominator);
cout << "The quotient is " << result << endl;
}
catch (runtime_error& e) {
cout << "Exception occurred" << endl << e.what();
}
}
OUTPUT:
enter the numerator 4
enter the denominator2
The quotient is 2

You might also like