0% found this document useful (0 votes)
63 views20 pages

Important OOPs Notes

This document contains a practical file of OOPS lab programs submitted by Sonu Tripathi with roll number 21001003910. It contains 12 programs covering concepts like power function, calculator, inheritance, constructors, operator overloading for complex, string and rational numbers, exception handling and more. Each program is accompanied by its source code and expected output.

Uploaded by

rehan rao
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)
63 views20 pages

Important OOPs Notes

This document contains a practical file of OOPS lab programs submitted by Sonu Tripathi with roll number 21001003910. It contains 12 programs covering concepts like power function, calculator, inheritance, constructors, operator overloading for complex, string and rational numbers, exception handling and more. Each program is accompanied by its source code and expected output.

Uploaded by

rehan rao
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/ 20

PRACTICAL FILE

OF
OOPS LAB PROGRAMS

SUBMITTED TO :
Miss. Pooja Sangwan
CSED DCRUST

SUBMITTED BY :
Sonu Tripathi
21001003910
B.tech 2nd year
S.No PROGRAMS Date Sign

1 Power Function
2 CALCULATOR

3 Responsive calculator

4 Multilevel Inheritance

5 Using virtual keyword

6 Constructor overloading

7 Distance calculations(m and f)

8 Overloading operators in Complex


Class (-, + , * , /, +=)

9 Overloading operators in String Class


(+,==)

10 Adding rational number

11 Employee Class data

12 Exceptions handling

1. Raising a number n to a power p is the same as multiplying n by itself p times.


Write a function called power ( ) that takes a double value for n and an int value
for p, and returns the result as double value
Use a default argument of 2 for p. so that if this argument is omitted, the number
will be squared. Write a main ( ) function that gets values from the user to test this
function.

#include<iostream>
using namespace std;
double pwr(int p,double n)
{

double result=1 ;

cout<<"do you want by default"<<endl;


char y;
cin>>y;
switch(y)
{
case ('y' || 'Y'):
break;
case ('n'):
int d;
cin>>d;
p=d;
break;
default :
break;

}
while(p>0)
{
result=result*n;
p--;
}

return result;
}

int main()
{
Cout<<”sonu”<<endl;
Cout<<”21001003910”<<endl;
int p;
double n;
cin>>n;
double result=pwr(p=2,n);
cout<<result;
}

Output
2. Create the equivalent of a four function calculator. The program
should request the user to enter a number, an operator, and another
number. It should then carry out the specified arithmetical operation:
adding. subtracting, multiplying, or dividing the two numbers. (It
should use a switch statement to select the operation). Finally it should
display the result.
#include<iostream>
using namespace std;
int main()
{

Cout<<”sonu”<<endl;
Cout<<”21001003910”<<endl;
cout<<"enter the first number"<<endl;
double first_number;
cin>>first_number;
cout<<"enter the your fav operator"<<endl;
char oper;
cin>>oper;
cout<<"enter the Second number"<<endl;
double second_number;
cin>>second_number;
double result;
switch(oper)
{
case ('-'):
result=first_number-second_number;
break;
case('/'):
result=first_number/second_number;
break;
case('*') :
result=first_number*second_number;
break;
case('+'):
result=first_number+second_number;
break;

default:
cout<<"operator overflow"<<endl;
break;
}

cout<<result<<endl;
}

OUTPUT

3. When it finishes the calculation, the program should ask if the user
wants to do another calculation.
The response can be 'Y' or 'N'. Some sample interaction with the
program might look like this.

#include<iostream>
using namespace std;
int main()
{
Cout<<”sonu”<<endl;
Cout<<”21001003910”<<endl;

while(true)
{
cout<<"enter the first number"<<endl;
double first_number;
cin>>first_number;
cout<<"enter the your fav operator"<<endl;
char oper;
cin>>oper;
cout<<"enter the Second number"<<endl;
double second_number;
cin>>second_number;
double result;
switch(oper)
{
case ('-'):
result=first_number-second_number;
break;
case('/'):
result=first_number/second_number;
break;
case('*') :
result=first_number*second_number;
break;
case('+'):
result=first_number+second_number;
break;

default:
cout<<"operator overflow"<<endl;
break;
}

cout<<result<<endl;

cout<<"do you want more calculation press y for yes and n for no"<<endl;
char more;
cin>>more;
if(more=='y' || more=='Y')
{
cout<<"\n";
}
else
{
break;

OUTPUT

4. To implement multi level inheritance.

#include <iostream>
using namespace std;

class MyClass {
public:
void myFunction() {
cout <<"Some content in parent class.\n" ;
}
};

class MyOtherClass {
public:
void myOtherFunction() {
cout <<"Some content in another class.\n" ;
}
};
class MyChildClass: public MyClass, public MyOtherClass {
};

int main() {
cout<<”sonu”<<endl;
cout<<”21001003910”<<endl;
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}

OUTPUT

5.Using Pointer For Base And Derivied Class And Call The Member Function Use
Virtual Keyword

#include <iostream>
using namespace std;
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()
{
Cout<<”sonu”<<endl;
Cout<<”21001003910”<<endl;
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
}
OUTPUT

6 To Implement Constructor Overloading

#include <iostream>
using namespace std;
class construct
{
public:
construct(int area)
{
cout<<area;
}
construct(int a, int b)
{
int area = a * b;
cout<<area;
}

};
int main()
{
Cout<<”sonu”<<endl;
Cout<<”21001003910”<<endl;
construct o(0);
construct o2( 10, 20);
}

OUTPUT

7. Create two classes DM and DB which store the value of distances. DM stores
distances in metres and centimeters and DB in feet and inches. Write a program
that can read values for the class objects and add one object of DM with another
object of DB. Use a friend function to carry out the addition operation. The object
that stores the results maybe DM object or DB object. depending on the units in
which the results are required. The display should be in the format of feet and
inches or metres and centimetres depending on object on display.

#include<iostream>
#include<cmath>
class DM;
using namespace std;
class DB{
public:
double feets,inches;
DB(double x,double y){
this->feets = x;
this->inches = y;
};
friend void add(DB,DM);

};
class DM{
public:
double meters,centimeters;
DM(double a,double b){
this->meters = a;
this->centimeters = b;
};
friend void add(DB,DM);
};
void add(DM ob1,DB ob2){
char c;
cout<<"Enter units for answer(F for feet & inches and M for meter & cm)";
cin>>c;
if(c=='F'){
double i=((ob1.meters*100)+ob1.centimeters)/2.54;
double j=(ob2.feets*12)+ob2.inches;
double sum=i+j;
double k=fmod(sum,12);
double l=(sum-k)/12;
cout<<"After add, Sum = "<<l<<" feets "<<k<<" inches ";
}
else if(c=='M'){
double i=(ob1.meters*100)+ob1.centimeters;
double j=((ob2.feets*12)+ob2.inches)*2.54;
double sum=i+j;
double k=fmod(sum,100);
double l=(sum-k)/100;
cout<<"After add, Sum = "<<l<<" meters "<<k<<" centimeters ";
}
else{
cout<<"Incorrect Units Entered";
}
}
int main(){
cout<<”sonu”<<endl;
cout<<”21001003910”<<endl;
cout<<"Enter distance(in meters and centimeters)"<<endl;
double a,b;
cin>>a>>b;
DM ob1(a,b);
cout<<"Enter distance(in feets and inches)"<<endl;
double x,y;
cin>>x>>y;
DB ob2(x,y);
add(ob1,ob2);
return 0;
}
OUTPUT

8. Write a Program to overload +,-,*,/,+= on a class of complex


numbers.

#include<iostream>
#include<string.h>
using namespace std;
class Complex
{
public:
int real;
int img;
Complex()
{
cout<<"";
}

Complex(int real ,int img )


{
this->real=real;
this->img=img;
}
Complex operator *(Complex c)
{
Complex res;
res.real=real*c.real -img*c.img;
res.img=real*c.img+ img*c.real;

return res;

Complex operator /(Complex c)


{
Complex res;
res.real=(real*c.real+img*c.img)/(c.real*c.real+c.img*c.img);
res.img=(img*c.real-real*c.img)/(c.real*c.real+c.img*c.img);
return res;
}
void operator +=(Complex c)
{

real=real+c.real;
img=img+c.img;
}

Complex operator +(Complex c)


{
real=real+c.real;
img=img+c.img;
return *this;
}

Complex operator -(Complex c)


{
real=real-c.real;
img=img-c.img;

return *this;
}
};

int main(){

cout<<”sonu”<<endl;
cout<<”21001003910”<<endl;
Complex c1(1,1),c2(1,1);
Complex res=c1*c2;
Complex c5=c1+c2;
Complex c4(2,3) ,c3(4,4);
Complex c7(1,1),c8(1,1);
Complex c6=c7-c8;
c4+= c3;
cout<<"+= overload"<<endl;
cout<<c4.real<<""<<c4.img<<endl;
cout<<"+ overload"<<endl;
cout<<c5.real<<""<<c5.img<<endl;
cout<<"- overload"<<endl;
cout<<c6.real<<""<<c6.img<<endl;
cout<<"* overload"<<endl;
cout<<res.real<<""<<res.img<<endl;

OUTPUT

9.Program to overload( +,==) on the class of string

#include<iostream>
#include<string.h>
using namespace std;
class Mystring
{

public:
char str[50];
Mystring()
{
cout<<"";
}
Mystring(char s[])
{
strcpy(str,s);

}
Mystring operator +(Mystring str1)
{
strcat(str,str1.str);
return *this;
}

int operator ==(Mystring str1)


{
int c=strcmp(str,str1.str)==0?1:0;
return c;
}

};
int main(){
cout<<”sonu”<<endl;
cout<<”21001003910”<<endl;

Mystring str1("delhi"),str2(" INDIA");


Mystring strs=str1+str2;
cout<<strs.str;
if(str1==str2)
cout<<"\nstring are equal\n";
else
cout<<"\nstring are not equal\n";

OUTPUT:
10.Create a class rational which represents a numerical value by
NUMERATOR & DENOMINATOR . Write a Program to overload( +,- ) for
class of rational

#include<iostream>
using namespace std;
int hcf(int a,int b){
int lowest=a>b?b:a;
while(true)
{
if(a%lowest==0 && b%lowest==0)
break;

lowest--;
}
return lowest;
}
class Rational{
private:
int p,q;
public:
Rational(int p,int q){
this->p = p;
this->q = q;
}
Rational(){}
Rational operator + (Rational r){
Rational res;
res.p = p*r.q + r.p*q;
res.q = q * r.q;
int h=hcf(res.p,res.q);
res.p /= h;
res.q /= h;
return res;
}
void printFraction(){
cout<<"Sum = "<<p<<" / "<<q;
}
};
int main(){
cout<<”sonu”<<endl;

cout<<”21001003910”<<endl;
int num1,den1,num2,den2,num3,den3;
cout<<" Enter 1st Fraction :(p q form) ";
cin>>num1>>den1;
cout<<" Enter 2nd Fraction :(p q form) ";
cin>>num2>>den2;
Rational r1(num1,den1);
Rational r2(num2,den2);
Rational r3 = r1 + r2;
r3.printFraction();
return 0;
}

OUTPUT

11. Make a class Employee with a name and salary. Make a class Manager inherit
from Employee. Add an instance variable, named department, of type string.
Supply a method to toString that prints the manager's name, department and
salary. Make a class Executive inherit from Manager Supply a method to String that
prints the string Executive followed by the information stored in the Manager
superclass object. Supply a test program that tests these classes and methods

#include<iostream>
using namespace std;
class Employee{
public:
char name[50]="Ramesh";
int salary=75700;
};
class Manager : public Employee{
public:
char department[50]="CSE";
void toString(){
cout<<"Manager Details:\n";
cout<<"Name: "<<name<<"\nSalary: "<<salary<<"\nDepartment:
"<<department<<"\n\n";
}
};
class Executive : public Manager{
public:
void toString(){
cout<<"Executive Details:\n";
cout<<"Executive "<<"Name: "<<name<<"\nSalary: "<<salary<<"\nDepartment:
"<<department<<"\n\n";
}
};

int main(){
cout<<”sonu”<<endl;
Manager mg;
Executive ex;
mg.toString();
ex.toString();
}

OUTPUT:
12.write a program to demonstrate exception handling.

#include<iostream>
Using namespace std;
Int main ()
{
Cout<<”sonu”<<endl;
Cout<<”21001003910”<<endl;
Int x=50;
Int y=0;
double z=0;
try {
if ( y==0)
throw “division by zero condition!”;
double res = x/y;
cout<<res;
}
Catch(const char*msg){
Cerr<<msg<<endl;
}
Return 0;
}

OUTPUT:

You might also like