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

Oops Practicals

The document contains 12 programs demonstrating various C++ concepts like classes, objects, functions, operator overloading, constructors etc. Program 1 inputs and displays the sum of different data types. Program 2 demonstrates call by value and reference using a swap function. Program 3 adds array elements and finds the average. Program 4 defines a power function using default arguments. Program 5 overloads the volume function for different shapes. Program 6 traverses an array using pointers. Program 7 defines a box class. Program 8 defines member functions inside and outside a class. Program 9 overloads constructors. Programs 10-12 demonstrate friend functions, addition of distances between classes, and operator overloading respectively.

Uploaded by

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

Oops Practicals

The document contains 12 programs demonstrating various C++ concepts like classes, objects, functions, operator overloading, constructors etc. Program 1 inputs and displays the sum of different data types. Program 2 demonstrates call by value and reference using a swap function. Program 3 adds array elements and finds the average. Program 4 defines a power function using default arguments. Program 5 overloads the volume function for different shapes. Program 6 traverses an array using pointers. Program 7 defines a box class. Program 8 defines member functions inside and outside a class. Program 9 overloads constructors. Programs 10-12 demonstrate friend functions, addition of distances between classes, and operator overloading respectively.

Uploaded by

ppawah04
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Program 1: Input 2 integers, float, character and display sum----------

#include <iostream>
using namespace std;
int main() {
int num1,num2;
float float1,float2;
char char1,char2;
cout<<"Enter the first number:";
cin>>num1;
cout<<"Enter the second number:";
cin>>num2;
int sumintegers = num1+num2;
cout<<"Sum of integer:"<<sumintegers<<endl;
cout<<"Enter the first float:";
cin>>float1;
cout<<"Enter the second float:";
cin>>float2;
float sumfloats = float1+float2;
cout<<"Sum of float:"<<sumfloats<<endl;
cout<<"Enter the first character:";
cin>>char1;
cout<<"Enter the second character:";
cin>>char2;
char sumcharacters = char1+char2;
cout<<"Sum of character:"<<sumcharacters<<endl;
return 0;
}

Output--------
Enter the first number:4
Enter the second number:3
Sum of integer:7
Enter the first float:5.6
Enter the second float:3.2
Sum of float:8.8
Enter the first character:g
Enter the second character:k
Sum of character:gk

Program 2: (a) Call by value------------


#include <iostream>
using namespace std;
void swap(int , int);
int main() {
int a,b;
cout<<"Enter value of A::";
cin>>a;
cout<<"Enter value of B::";
cin>>b;
cout<<"\n before swapping value of ::\n\tA="<<a<<"\tB="<<b<<"\n";
swap(a,b);
cout<<"\n outside function after swapping value of ::\n\tA="<<a<<"\tB="<<b<<"\n";
}
void swap(int a , int b)
{
int c;
c=a;
a=b;
b=c;
cout<<"\n inside function after swapping value of ::\n\tA="<<a<<"\tB="<<b<<"\n";
}

Output--------
Enter value of A::3
Enter value of B::6
before swapping value of ::
A=3 B=6
inside function after swapping value of ::
A=6 B=3
outside function after swapping value of ::
A=3 B=6

(b) Call by reference---------


#include <iostream>
using namespace std;
void swap(int*,int*);
int main() {
int a =1,b=2;
cout<<"Before swapping:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
swap(&a,&b);
cout<<"\n After swapping:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
return 0;
}
void swap(int*n1,int*n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
}

output--------
Before swapping:
a=1
b=2
After swapping:
a=2
b=1

Program 3: (a)add array elements and print its average-----------


#include <iostream>
using namespace std;
int main() {
int i, size;
double sum=0,average=0;
cout<<"\n\n Enter the size of an array:";
cin>>size;
int arr[size];
cout<<"\n\n Enter"<<size << "int of an array:\n\n";
for(i=0; i<size ; i++)
{
cout<<"Enter arr["<<i<<"]:";
cin>>arr[i];
}
cout<<"\n\n The elements of the array are :\n\n";
for (i=0; i=size ; i++)
{
cout<<"arr["<<i<<"]="<<arr[i]<<endl;
sum+=arr[i];
}
average = sum/size;
cout<<"\n\n The sum of the elements of the array is :"<<sum<<"\n\n";
cout<<"\n\n The average of the elements of the array is :"<<average<<"\n\n";
cout<<"\n\n";
return 0;
}
Output---------
Enter the size of an array:3
Enter3int of an array:
Enter arr[0]:1
Enter arr[1]:2
Enter arr[2]:3
The elements of the array are :
arr[0]=1
arr[1]=2
arr[2]=3
The sum of the elements of the array is :6
The average of the elements of the array is :2

(b) reverse array elements using while loop------------


#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the number of elements:";
cin>>n;
int arr[n];
cout<<"Enter"<<n<<"elements:"<<endl;
int i=0;
while(i<n)
{
cin>>arr[i];
i++;
}
cout<<"Elements in reverse order:"<<endl;
i=n-1;
while(i>=0){
cout<<arr[i]<<" ";
i--;
}
return 0;
}

Output---------
Enter the number of elements:3
Enter3elements:
1
2
3
Elements in reverse order:
321

Program 4--------
#include <iostream>
using namespace std;
double power (double, int =2);
int main() {
int p;
double n,r;
cout<<"Enter number:";
cin>>n;
cout<<"Enter exponent:";
cin>>p;
r=power(n,p);
cout<<"Result is :"<<r;
cout<<"\n Result without passing exponent is "<<r;
return 0;
}
double power (double a, int b)
{
double x=1;
for(int i=1;i<=b;i++)
x=x*a;
return x;
}

Output---------
Enter number:5
Enter exponent:4
Result is :625
Result without passing exponent is 625

Program 5: volume of cube, cylinder, rectangle using function overloading----------


#include <iostream>
using namespace std;
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
cout<<volume (10)<<"\n";
cout<<volume (2.5,8)<<"\n";
cout<<volume (100l,75,15)<<"\n";
return 0;
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14519*r*r*h);
}
long volume(long l, int b, int h)
{
return(l*b*h);
}

Output--------
1000
157.26
112500

Program 6: traverse an array using pointer notations and also copy contents of one
array into another---------
#include <iostream>
using namespace std;
int main()
{
int*p;
int a[4]={44,55,6,4},b[4];
p=a;
p=&a[0];
for(int i=0;i<4;i++)
{
cout<<*(a+i)<<"\n";
}
for(int i=0;i<4;i++)
{
*(b+i)=*(b+i);
cout<<*(b+i);
}
return 0;
}

Output----------
44
55
6
4
445564

Program 7: design box class with three data mem bers l,b,h and also find volume and
area-----------
#include <iostream>
using namespace std;
class Box
{
int l,b,h;
public:
void volume()
{
cout<<"Enter length,breadth and height of box:";
cin>>l>>b>>h;
cout<<"Volume of box:"<<l*b*h<<endl;
}
void area()
{
cout<< "Area of box:"<<l*b<<endl;
}
};
int main() {
Box b1;
b1.volume();
b1.area();
return 0;
}

Output----------
Enter length, breadth and height of box:2 3 4
Volume of box:24
Area of box:6
Program 8:(a) define member function inside the class---------
#include <iostream>
using namespace std;
class Car
{
int speed;
int mileage;
public:
void maxspeed()
{
cout<<"Enter max speed of the car:";
cin>>speed;
cout<<"max allowed speed:"<<speed<<endl;
}
void maxmileage()
{
cout<<"Enter max mileage of the car:";
cin>>mileage;
cout<<"max mileage of the car:"<<mileage<<endl;
}
};
int main() {
Car c1;
c1.maxspeed();
c1.maxmileage();
return 0;
}

Output-----------
Enter max speed of the car:40
max allowed speed:40
Enter max mileage of the car:18
max mileage of the car:18
(b) outside the class using scope resolution operator----------
#include <iostream>
using namespace std;
class Car
{
int speed;
int mileage;
public:
void maxspeed();
void maxmileage();
};
void Car::maxspeed()
{
cout<<"Enter max speed of the car:";
cin>>speed;
cout<<"max allowed speed:"<<speed<<endl;
}
void Car:: maxmileage()
{
cout<<"Enter max mileage of the car:";
cin>>mileage;
cout<<"max mileage of the car:"<<mileage<<endl;
}
int main() {
Car c1;
c1.maxspeed();
c1.maxmileage();
return 0;
}

Output--------
Enter max speed of the car:40
max allowed speed:40
Enter max mileage of the car:18
max mileage of the car:18

Program 9: Constructor Overloading-------------


#include <iostream>
using namespace std;
class Point
{
int x,y;
public:
Point ()
{
x=0;y=0;
}
Point(int x1,int y1)
{
x=x1;
y=y1;
}
Point (Point & obj)
{
x=obj.x;
y=obj.y;
}
void putdata()
{
cout<<"\n Value of x and y="<<x<<" " <<y;
}
};
int main() {
Point a;
Point ab(10,20);
Point c (ab);
a.putdata();
ab.putdata();
c.putdata();
return 0;
}

Output------------
Value of x and y=0 0
Value of x and y=10 20
Value of x and y=10 20

Program 10: WAP to use friend function to find time to travel-------------


#include <iostream>
using namespace std;
class Travel
{
private:
double speed,distance;
public:
void set_values (double a, double b)
{
speed = a;
distance = b;
}
friend double findTimeofTravel (Travel);
};
double findTimeofTravel (Travel t)
{
double time = t.distance / t.speed;
return time;
}
int main ()
{
Travel t;
t.set_values(10.5,30.5);
cout << "Time of Travel: " << findTimeofTravel (t) << " hrs" << endl;
return 0;
}

Output---------
Time of Travel: 2.90476 hrs

Program 11: Create two classes DM and DB which stores the value of distances. WAP
that can read values for the class objects and add one object DM with another object
DB. Use friend function to carry out addition------------------
#include <iostream>
using namespace std;
class DB;
class DM
{
int m,cm;
public:
void get_data()
{
cout<<"\nEnter distance in meters-centimetres: ";
cin>>m>>cm;
}
friend float sum(DM a,DB b);
};
class DB
{
int ft,in;
public:
void get_data()
{
cout<<"\nEnter distance feet-inches : ";
cin>>ft>>in;
}
friend float sum(DM a,DB b);
};
float sum(DM a,DB b)
{
float x,y,z;
x=(a.m+(a.cm/100));
y=(b.ft+(b.in/12));
z=(x+(y*0.304)); //Since 1 ft = 0.304 m
return z;
}
int main()
{
DM a;
DB b;
a.get_data();
b.get_data();
cout<<"\nThe summed value in meter is: "<<sum(a,b);
return 0;
}

Output--------------
Enter distance in meters-centimetres: 2 3
Enter distance feet-inches: 3 1
The summed value in meter is: 2.912

Program 12: WAP to overload unary increment prefix and postfix operator--------------
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5) {}
void operator ++ () {
++value;
}
void operator ++ (int) {
value++;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1++;
count1.display();
++count1;
count1.display();
return 0;
}

Output-----------
Count: 6
Count: 7

Program 13: WAP to overload + binary operator---------------


#include <iostream>
using namespace std;
class addobj{
int x,y;
public:
void get(){
cout<<"Enter the values of x and y:"<<endl;
cin>>x>>y;
}
void put(){
cout<<"x:"<<x<<endl;
cout<<"y:"<<y<<endl;
}
addobj operator+(addobj P2){
addobj resP3;
resP3.x=P2.x+x;
resP3.y=P2.y+y;
return resP3;
}
};
int main() {
addobj P1,P2,P3;
P1.get();
P2.get();
P3=P1+P2;
cout<<"The new x and y are:"<<endl;
P3.put();
return 0;
}

Output---------------
Enter the values of x and y:
37
Enter the values of x and y:
21
The new x and y are:
x:5
y:8

Program 14---------------
#include <iostream>
using namespace std;
class Number{
protected:
int x,y;
public:
void get(){
cout<<"Enter two number:"<<endl;
cin>>x>>y;
}
};
class Product:public Number
{
public:
void printProduct()
{
cout<<"Product:"<<x*y<<endl;
}
};
class Sum:public Number
{
public:
void printSum()
{
cout<<"Sum:"<<x+y;
}
};
int main() {
Product p;
Sum s;
p.get();
p.printProduct();
s.get();
s.printSum();
return 0;
}

Output----------
Enter two number:
34
Product:12
Enter two number:
23
Sum:5

Program 15: Multilevel inheritance---------


#include <iostream>
using namespace std;
class Student
{
private:
char name[20];
int rno;
int sem;
char stream[4];
public:
void acceptname()
{
cout<<" Enter the name:"<<endl;
cin>>name;
cout<<" Enter the rollno:"<<endl;
cin>>rno;
cout<<" Enter the semester:"<<endl;
cin>>sem;
cout<<" Enter the stream:"<<endl;
cin>>stream;
}
void displayname()
{
cout<<"Name:"<<name<<endl;
cout<<"Roll NO.:"<<rno<<endl;
cout<<"Semester:"<<sem<<endl;
cout<<"Stream:"<<stream<<endl;
}
};
class Marks:public Student
{
public:
int mark1,mark2,mark3;
void acceptmark()
{
cout<<"Enter assignment1 , housetest1 and housetest2 marks:";
cin>>mark1>>mark2>>mark3;
}
void displaymark()
{
cout<<"Marks Obtained"<<endl;
cout<<"assignment1:"<<mark1<<endl;
cout<<"housetest1:"<<mark2<<endl;
cout<<"housetest2:"<<mark3<<endl;
}
};
class Result:public Marks
{
int total;
public:
void showresult()
{
total=mark1+mark2+mark3;
cout<<"Total marks score:"<<total;
}
};
int main()
{
Result r1;
r1.acceptname();
r1.acceptmark();
r1.displayname();
r1.displaymark();
r1.showresult();
return 0;
}

Output-----------
Enter the name:
riya
Enter the rollno:
32
Enter the semester:
4
Enter the stream:
it
Enter assignment1, housetest1 and housetest2 marks:10 15 15
Name:riya
Roll NO.:32
Semester:4
Stream:it
Marks Obtained
assignment1:10
housetest1:15
housetest2:15
Total marks score:40

Program 16:hybrid diamond problem -----------


#include <iostream>
using namespace std;
class Student
{
char name[20];
char stream[5];
int rno;
int sem;
public:
void get()
{
cout<<"Enter name, stream ,rno and semester:";
cin>>name>>stream>>rno>>sem;
}
void put()
{
cout<<"Student Details"<<endl;
cout<<name<<endl;
cout<<stream<<endl;
cout<<rno<<endl;
cout<<sem<<endl;
}
};
class Academicmarks:virtual public Student
{
protected:
float ht1,ht2,ass;
public:
void accept()
{
cout<<"Enter the marks of housetest1, housetest2,assignment marks:";
cin>>ht1>>ht2>>ass;
}
void display()
{
cout<<"Marks Details" <<endl;
cout<<ht1<<endl;
cout<<ht2<<endl;
cout<<ass<<endl;
}
};
class Sports:virtual public Student
{
protected:
int score;
public:
void getdata()
{
cout<<"Enter the sports score:";
cin>>score;
}
void putdata()
{
cout<<"Sports score Details";
cout<<score;
}
};
class Result:public Sports,public Academicmarks
{
int total;
public:
void showresult()
{
total= ht1+ht2+ass+score;
cout<<"Total marks:"<<total;
}
};
int main() {
Result r1;
r1.get();
r1.accept();
r1.getdata();
r1.put();
r1.display();
r1.putdata();
r1.showresult();
return 0;
}

Output-----------
Enter name, stream, rno and semester: Teena it 31 3
Enter the marks of housetest1, housetest2, assignment marks:10 15 15
Enter the sports score:400
Student Details
teena
it
31
3
Marks Details
10
15
15
Sports score Details400
Total marks:440

Program 17:virtual function--------------


#include <iostream>
using namespace std;
class base{
public:
virtual void print()
{
cout<<"Print base class \n";
}
void show()
{
cout<<"Show base class \n";
}
};
class derived:public base
{
void print()
{
cout<<"Print derived class \n";
}
void show()
{
cout<<"Show derived class \n";
}
};
int main() {
base*bptr;
derived d;
bptr=&d;
bptr->print();
bptr->show();
return 0;
}

Output-------------
Print derived class
Show base class

Program 18:Pure virtual function-------------


#include <iostream>
using namespace std;
// Abstract class
class Shape
{
public:
virtual float calculateArea() = 0; // pure virtual function.
};
class Square : public Shape
{
float a;
public:
Square(float l)
{
a = l;
}
float calculateArea()
{
return a*a;
}
};
class Circle : public Shape
{
float r;
public:

Circle(float x)
{
r = x;
}
float calculateArea()
{
return 3.14*r*r ;
}
};
class Rectangle : public Shape
{
float l;
float b;
public:
Rectangle(float x, float y)
{
l=x;
b=y;
}
float calculateArea()
{
return l*b;
}
};
int main()
{

Shape *shape;
Square s(3.4);
Rectangle r(5,6);
Circle c(7.8);
shape =&s;
int a1 =shape->calculateArea();
shape = &r;
int a2 = shape->calculateArea();
shape = &c;
int a3 = shape->calculateArea();
cout << "Area of the square is " <<a1<< endl;
cout << "Area of the rectangle is " <<a2<< endl;
cout << "Area of the circle is " <<a3<< endl;
return 0;
}

Output---------------
Area of the square is 11
Area of the rectangle is 30
Area of the circle is 191

Program 19: (a) function template------------


#include <iostream>
using namespace std;
template<class T>
void swapme(T &x, T &y)
{
T temp = x;
x = y;
y = temp;
}
int main()
{
int a, b;
cout<<"Enter two integer values: ";
cin>>a>>b;
swapme(a,b);
cout<<"After swap a = "<<a<<", b = "<<b<<endl;
swapme(a,b);
double p, q;
cout<<"Enter two double values: ";
cin>>p>>q;
swapme(p, q);
cout<<"After swap p = "<<p<<", q = "<<q<<endl;
swapme(p, q);
string mr="Krishna" , mrs="Radha" ;
cout<<"before Swap"<<endl;
swapme(mr,mrs);
cout<<"Before swap mr= "<<mr<<", mrs= "<<mrs<<endl;
cout<<"after Swap"<<endl;
swapme(mr,mrs);
cout<<"After swap mr= "<<mr<<", mrs= "<<mrs<<endl;
return 0;
}

Output--------------
Enter two integer values: 2 3
After swap a = 3, b = 2
Enter two double values: 4.7 3.2
After swap p = 3.2, q = 4.7
before Swap
Before swap mr= Radha, mrs= Krishna
after Swap
After swap mr= Krishna, mrs= Radha

(b) class template------------------


#include <iostream>
using namespace std;
template <class T>
class Calculator {
private:
T num1, num2;
public:
Calculator(T n1, T n2) {
num1 = n1;
num2 = n2;
}
void displayResult() {
cout << "Numbers: " << num1 << " and " << num2 << "." << endl;
cout << num1 << " + " << num2 << " = " << add() << endl;
cout << num1 << " - " << num2 << " = " << subtract() << endl;
cout << num1 << " * " << num2 << " = " << multiply() << endl;
cout << num1 << " / " << num2 << " = " << divide() << endl;
}
T add() { return num1 + num2; }
T subtract() { return num1 - num2; }
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};
int main() {
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Int results:" << endl;
intCalc.displayResult();
cout << endl
<< "Float results:" << endl;
floatCalc.displayResult();
return 0;
}

Output----------------
Int results:
Numbers: 2 and 1.
2+1=3
2-1=1
2*1=2
2/1=2

Float results:
Numbers: 2.4 and 1.2.
2.4 + 1.2 = 3.6
2.4 - 1.2 = 1.2
2.4 * 1.2 = 2.88
2.4 / 1.2 = 2

Program 20: File Handling------------------


#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream MyFile("Student.txt");
string name="Teena";
float n=5.6;
int a=9;
MyFile << name<<n<<a;
MyFile.close();
ifstream i("Student.txt");
cout<<"Contents are:"<< name<<endl<<n<<endl<<a;
i.close();
}

Output---------
Contents are:Teena
5.6
9

You might also like