[go: up one dir, main page]

0% found this document useful (0 votes)
569 views18 pages

OOPS Project File

This document contains a lab record for an Object Oriented Programming course. It includes 15 programming problems addressing concepts like classes, objects, inheritance, constructors, destructors, and operator overloading. Each problem is numbered and includes the problem statement and code solution.

Uploaded by

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

OOPS Project File

This document contains a lab record for an Object Oriented Programming course. It includes 15 programming problems addressing concepts like classes, objects, inheritance, constructors, destructors, and operator overloading. Each problem is numbered and includes the problem statement and code solution.

Uploaded by

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

Dr. B.R.

Ambedkar National Institute Of Technology,


Jalandhar, 144011
Practical Lab Record
Instrumentation and Control Engineering

Department of Instrumentation and Control Engineering

OBJECT ORIENTED PROGRAMMING


(CSX-207)

Submitted To: Submitted By:


Mayur Kashyap
Nitin Sir 17106043
(Assistant Professor) Group-G2
Dept. Of CSE

Page 1 of 18
INDEX
S.No. Programs Page No.

1. Program to read a matrix of size m x n form the keyboard and display the 3
same using function.

2. Program to make the use of inline function. 4

3. A function power () which raise a number m to a power n. 5

Program to show that the effect of default arguments can be alternatively


4. achieved by overloading. 6

5. Write a class ACCOUNT that represents your bank account and then use it. 7-8

Write a class STRING that can be used to store strings, add strings, equate
6. string, output strings. 9

Create the class TIME to store time in hours and minutes. Write a friend
7. function to add two TIME objects 10

8. Program to demonstrate the concept of Constructor Overloading 11

9. Program to demonstrate the concept of destructor. 12

10. Program to show multiple inheritance 13

11. Program to show multilevel inheritance 14

12. Program to show hybrid inheritance 15

13. Program to overload unary operator. 16

14. Program to overload binary operator. 17

15. Program to show Abstract Class 18

Page 2 of 18
 Write a program to read a matrix of size m x n form the keyboard
and display the same using function.
#include<iostream> displaymatrix()
using namespace std; {
class matrix cout<<"The Matrix is\n";
{ for(int i=0;i<m;i++)
int a[5][5],m,n; {
public: {
addmatrix() for(int j=0;j<n;j++)
{ cout<<a[i][j]<<" ";
cout<<"Enter the Values of m and }
n\n"; cout<<"\n";
cin>>m>>n; }
cout<<"\n Enter the Matrix";
for(int i=0;i<m;i++) }
{ };
for(int j=0;j<n;j++) int main()
cin>>a[i][j]; {
} matrix m;
} m.addmatrix();
m.displaymatrix();
}

Page 3 of 18
 Program to make the use of inline function .

#include<iostream>
using namespace std;
inline float mul(float x,float y)
{
cout << "Multiplication of numbers is: " << x*y << endl;
}
int main()
{
float a=10.4,b=15.24;
mul(a,b);
return 0;
}

Page 4 of
18
 Write a function power () which raise a number m to a power n. The
function takes double value of m and integer value of n and returns the
result. Use a default value of n is 2 to make the function to calculate
squares when this argument is omitted.
#include<iostream>
#include<math.h>
using namespace std;
double power(double m,int n)
{
double mm=pow(m,n);
return mm;
}
double power(double m,long int n=2)
{
double mm=pow(m,n);
return mm;
}
int main()
{
double mm;
double m;
int n;
cout<<" Enter the value of m & n\n";
cin>>m>>n;
mm=power(m,n);
cout<<" m raise to power n(by two arguments) :
"<<mm; mm=power(m);
cout<<" \nm to power n (by one argument) : "<<mm;
return 0;
}

Page 5 of
18
 Program to show that the effect of default arguments can be
alternatively achieved by overloading.
#include<iostream>
using namespace std;
void multiplication(int x,int y)
{
cout << "Multiplication of two integers is: " << x*y ;
}
void multiplication(int a,double b,float c)
{
cout << "\nMultiplication of three integers is: " << a*b*c;
}
int main()
{
multiplication(10,15);
multiplication(5,4.5,);
return 0;
}

Page 6 of
18
 Write a class ACCOUNT that represents your bank account and then use it.
The class should allow you to deposit money, withdraw money, calculate
interest, send you a message if you have insufficient balance.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class bank
{
int acno;
char nm[100], acctype[100];
float bal;
public:
bank(int acc_no, char *name, char *acc_type, float balance)
{
acno=acc_no;
strcpy(nm, name);
strcpy(acctype, acc_type);
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void bank::deposit()
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void bank::withdraw()
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void bank::display()
{
cout<<"\n ----------------------";
cout<<"\n Accout No. : "<<acno;
cout<<"\n Name : "<<nm;
cout<<"\n Account Type : "<<acctype;
cout<<"\n Balance : "<<bal;

}
Page 7 of
18
int main()
{
int acc_no;
char name[100], acc_type[100];
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Accout No. ";
cin>>acc_no;
cout<<"\n Name : ";
cin>>name;
cout<<"\n Account Type : ";
cin>>acc_type;
cout<<"\n Balance : ";
cin>>balance;
bank b1(acc_no, name, acc_type,balance);
b1.deposit();
b1.withdraw();
b1.display();
return 0;
}

Page 8 of
18
 Write a class STRING that can be used to store strings, add strings, equate
string, output strings.
#include <iostream>
using namespace std;
void compareFunction(string s1, string s2)
{
int x = s1.compare(s2);
if (x != 0)
cout << s1 << " is not equal to "<< s2 ;
if (x > 0)
cout<< “\n”<< s2 << " is greater than "<< s1;
else
cout <<”\n”<< s1 << " is greater than "<< s2;
}
int main()
{
string s1("Mayur");
string s2("Kashyap");
compareFunction(s1, s2);
return 0;
}

Page 9 of
18
 Create the class TIME to store time in hours and minutes. Write a
friend function to add two TIME objects.

#include<iostream>
using namespace std;
class time
{
int hours,minutes;
public:
time(int m)
{
hours=m/60;
minutes=m%60;
}
void show()
{
cout<<"time in hours and minutes\n"<<hours<<":"<<minutes;
}
};
int main()
{
int minutes=90;
time t=minutes;
t.show();
return 0;
}

Page 10 of 18
 Program to demonstrate the concept of:
a. Default constructor
b. Parameterized constructor
c. Copy constructor
d. Constructor overloading
#include<iostream>
using namespace std;
class Area
{
public:
float area;
Area()
{
area=10.4;
}
Area(int a,int b)
{
area=a*b;
}
void display()
{
cout << "Area: " << area << endl << endl;
}
};
int main()
{
Area A1;
Area A2(5,10);
A1.display();
A2.display();
return 0;
}

Page 11 of 18
 Program to demonstrate the concept of destructor.

#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Constructor called\n\n";
}
~Base()
{
cout << "Destructor called\n";
}
};
int main()
{
Base B;
return 0;
}

Page 12 of 18
 Program to show multiple inheritance

#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"A' constructor is called"<<"\n";
}
};
class B
{
public:
B()
{
cout<<"B' constructor is called"<<"\n";
}
};
class C:public B,public A
{
public:
C()

cout<<"C' constructor is called"<<"\n";


}
};
int main()
{
C c;
return 0;
}

Page 13 of 18
 Program to show multilevel inheritance
#include<iostream> };
using namespace std; class rectangle:public perimeter
class Area {
{ private:
public: int A=0;
int l; public:
void getdata() void putdata()
{ {
cout<<"enter the length="; A=l*b;
cin>>l; cout<<"\n\nArea of
} rectangle="<<A<<"\n";
}; }
class perimeter:public Area };
{ int main()
public: {
int b; rectangle r;
void readata() r.getdata();
{ r.readata();
cout<<"\nenter the breadth="; r.putdata();
cin>>b; return 0;
} }

Page 14 of 18
 Program to show hybrid inheritance

#include<iostream> C()
using namespace std; {
class A y=4;
{ }
public: };
int x; class D: public B,public C
}; {
class B:public A public:
{ void mul()
public: {
B() cout<<"multiplication of x and y
{ = "<<x*y<<"\n";
x=10; }
} };
}; int main()
class C {
{ D a;
public: a.mul();
int y; return 0;
}

Page 15 of 18
 Program to overload unary operator.
#include<iostream>
using namespace std;
class number
{
int x;
public:
void getnumber(int a)
{
x=a;
cout << "Before overloading unary operator, value of number is: " << x << endl;
}
void operator-()
{
x=-x;
}
void shownumber()
{
cout << "\nAfter overloading unary operator, value of number is: " << x << "\n\n" ;
}
};
int main()
{
number N;
N.getnumber(10);
-N;
N.shownumber();
return 0;
}

Page 16 of 18
 Program to overload binary operator

#include<iostream>
using namespace std;
class complex
{
float x,y;
public:
complex() { }
complex(float r,float i)
{
x=r; y=i;
}
complex operator+(complex c)
{
complex a;
a.x = x+c.x;
a.y = y+c.y;
return(a);
}
void display()
{
cout << x << " +j" << y << "\n";
}
};
int main()
{
complex C1(2.4,5.2),C2(4.1,5.7);
complex C3 = C1+C2;
cout << "C1 = "; C1.display();
cout << "C2 = "; C2.display();
cout << "\nC3 = "; C3.display();
return 0;
}

Page 17 of 18
 Program to show Abstract Class

#include<iostream>
using namespace std;
class B
{
int x;
public:
virtual void fun()=0;
int getX()
{
return x;
}
};
class D:public B
{
int y;
public:
void fun()
{
cout<<"fun() called"<<"\n";
}
};
int main(void)
{
D t;
t.fun();
return 0;
}

Page 18 of 18

You might also like