Practical No.
1
Create a simple class with data members and member functions. To
demonstrate use of class instances (object) access data and invoke member
function.
Code:-
#include <iostream>
using namespace std;
class item
{
int number;
float cost;
public:
void getdata(int a, float b){number=a;cost-b;}
void putdata(){cout<<"\nnumber is:"<<number;
cout<<"\ncost is:"<<cost;}
};
int main()
{
item x;
cout<<"For object x:";
x.getdata(100,299.95);
x.putdata();
cout<<"\nFor object y:";
item y;
y.getdata(200, 179.50);
y.putdata();
return 0;
}
Output:-
Practical No.2
Implement classes to demonstrate single inheritance.
Code:-
#include <iostream>
using namespace std;
class B
{
int a;
public:
int b;
void getvalueab(){a=5;b=10;}
int getvaluea(){return a;}
void showa(){cout<<"a is: "<<a;}
};
class D: public B
{
int c;
public:
void mul(){c=b*getvaluea();}
void display(){cout<<"\na is:"<<getvaluea();
cout<<"\n b is:"<<b;
cout<<"\n c is:"<<c;}
};
int main()
{
D d;
d.getvalueab();
d.mul();
d.display();
return 0;
}
Output:-
Practical No.3
Implement classes to demonstrate multilevel inheritance.
Code:-
#include <iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
void getrollno(int a){rollno=a;}
void putrollno(){cout<<"\nRoll number is:"<<rollno;}
};
class test: public student
{
protected:
float sub1,sub2;
public:
void getmarks(float x, float y){sub1=x; sub2=y;}
void putmarks(){cout<<"\nmarks in sub1 is:"<<sub1;
cout<<"\nmarks in sub2 is:"<<sub2;}
};
class result:public test
{
float total;
public:
void displaytotal(){total=sub1+sub2;
putrollno();
putmarks();
cout<<"\ntotal is="<<total;}
};
int main()
{
result student1;
student1.getrollno(1);
student1.getmarks(75,85);
student1.displaytotal();
return 0;
}
]
Output:-
Practical No. 4
Implement classes to demonstrate multiple inheritance.
Code:-
#include <iostream>
using namespace std;
class M
{
protected:
int m;
public:
void getm(int a){m=a;}
};
class N
{
protected:
int n;
public:
void getn(int b){n=b;}
};
class P: public M, public N
{
public:
void display(){ cout<<"\nvalue of m is:"<<m;
cout<<"\nvalue of n is:"<<n;
cout<<"\nvalue of man is:"<<m*n;}
};
int main()
{
P p1;
p1.getm(20);
p1.getn(30);
p1.display();
return 0;
}
Output:-
Practical No. 5
Implement classes to implement hybrid inheritance.
Code:-
#include <iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
void getrollno(int a) {rollno=a;}
void putrollno() { cout<<"Roll no is:"<<rollno;}
};
class test:public student
{
protected:
float sem1, sem2;
public:
void getmarks(float x, float y){sem1=x; sem2=y;}
void putmarks(){cout<<"\nmarks in sem1:"<<sem1;
cout<<"\nmarks in sem2:"<<sem2;}
};
class sports
{
protected:
float score;
public:
void getscore(float s){score=s;}
void putscore() { cout<<"sports weight is:"<<score;}
};
class result: public test, public sports
{
float total;
public:
void display(){total=sem1+sem2+score;
putrollno();
putmarks();
putscore();
cout<<"\n total score is:"<<total;}
};
Output:-
Practical No. 6
Constructor And Destructor
Code:-
#include <iostream>
using namespace std;
class Integer {
int m, n; // Private data members
public:
Integer(int x, int y); // Constructor declaration
void display(); // Function to display values
~Integer(); // Destructor declaration
};
// Constructor definition
Integer::Integer(int x, int y) {
m = x;
n = y;
cout << "Constructor called. Values initialized: " << m << ", " <<
n << endl;
}
// Function to display values
void Integer::display() {
cout << "Values: " << m << ", " << n << endl;
}
// Destructor definition
Integer::~Integer() {
cout << "Destructor called. Object is destroyed." << endl;
}
int main() {
cout << "Creating Object 1:" << endl;
Integer int1(100, 200); // Constructor called implicitly
int1.display();
cout << "Creating Object 2:" << endl;
Integer int2(300, 400); // Constructor called explicitly
int2.display();
return 0;
}
Output:-
Practical No. 7.1
Public access specifier
Code:-
#include <iostream>
using namespace std;
class xyz {
public:
int x; // Public variable x
void display() {
cout << "\nValue of x is: " << x;
}
};
int main() {
xyz a1; // Object a1 of class xyz
a1.x = 25; // Public member x can be accessed directly
a1.display();
return 0;
}
Output:-
Practical No. 7.2
Private access specifier
Code:-
#include <iostream>
using namespace std;
class xyz {
private:
int x; // Private variable x
public:
// Member function to assign value to x
void getdata(int a) {
x = a; // Assigning value to private variable
}
// Member function to display value of x
void display() {
cout << "\nValue of x is: " << x << endl;
}
};
int main() {
xyz a1; // Object a1 of class xyz defined
a1.getdata(25); // Assign value using member function
a1.display(); // Display value using member function
return 0;
}
Output:-
Practical No. 7.3
Protected access specifier
Code:-
#include <iostream>
using namespace std;
// Base class
class xyz {
protected:
int x; // Protected variable x
public:
void getdata(int a) { x = a; } // Function to assign value to x
};
// Derived class
class abc : public xyz {
public:
void display() {
cout << "\nValue of x is: " << x; // Accessing protected member
}
};
int main() {
abc a1; // Object a1 of derived class abc
a1.getdata(25); // Accessing base class function to set x
a1.display(); // Accessing derived class function to display x
return 0;
}
Output:-
Practical No. 8
Use of Scope Resolution Operator
Code:-
#include <iostream>
using namespace std;
class integer {
int m, n; // Private data members
public:
void getdata(int, int); // Function prototype
void display(); // Function prototype
};
// Scope resolution operator used to define member functions outside
the class
void integer::getdata(int a, int b) {
m = a;
n = b;
}
void integer::display() {
cout << "\nValue of m is: " << m;
cout << "\nValue of n is: " << n;
}
int main() {
integer int1; // Object int1 of class integer
int1.getdata(100, 200); // Assign values using member function
int1.display(); // Display values using member function
return 0;
}
Output:-
Practical No. 9
Function Overloading
Code:-
#include <iostream>
using namespace std;
class dimension {
public:
// Function to calculate volume of a cube
int volume(int a) {
return (a * a * a);
}
// Function to calculate volume of a cylinder
double volume(double r, int h) {
return (3.14 * r * r * h);
}
// Function to calculate volume of a rectangular box
int volume(int l, int b, int h) {
return (l * b * h);
}
};
int main() {
dimension d; // Object of class dimension
cout << "\nVolume of cube is: " << d.volume(10);
cout << "\nVolume of cylinder is: " << d.volume(2.5, 8);
cout << "\nVolume of rectangular box is: " << d.volume(100, 75,
15);
return 0;
}
Output:-
Practical No. 10
Operator Overloading
Code:-
#include <iostream>
using namespace std;
class space {
int x, y, z;
public:
void getdata(int a, int b, int c); // Function prototype
void display(); // Function prototype
void operator-(); // Operator overloading prototype
};
// Function to set values
void space::getdata(int a, int b, int c) {
x = a;
y = b;
z = c;
}
// Function to display values
void space::display() {
cout << x << " " << y << " " << z << endl;
}
// Overloading the minus (-) operator to perform addition
void space::operator-() {
x = x + y;
y = y + z;
z = z + x;
}
int main() {
space m;
m.getdata(10, 20, 30);
cout << "Original values: ";
m.display();
-m; // Overloaded minus operator
cout << "After overloading - operator: ";
m.display();
return 0;
}
Output:-
Practical No. 11.1
Operator Overloading Without Virtual Function.
Code:-
#include <iostream>
using namespace std;
class space {
int x, y, z;
public:
void getdata(int a, int b, int c); // Function prototype
void display(); // Function prototype
void operator-(); // Operator overloading prototype
};
// Function to set values
void space::getdata(int a, int b, int c) {
x = a;
y = b;
z = c;
}
// Function to display values
void space::display() {
cout << x << " " << y << " " << z << endl;
}
// Overloading the minus (-) operator to perform addition
void space::operator-() {
x = x + y;
y = y + z;
z = z + x;
}
int main() {
space m;
m.getdata(10, 20, 30);
cout << "Original values: ";
m.display();
-m; // Overloaded minus operator
cout << "After overloading - operator: ";
m.display();
return 0;
}
Output:-
Practical No. 11.2
Operator Overloading With Virtual Function.
Code:-
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() { cout << "\nDisplay Base"; }
virtual void show() { cout << "\nShow Base"; }
};
class Derived : public Base {
public:
void display() override { cout << "\nDisplay Derived"; }
void show() override { cout << "\nShow Derived"; }
};
int main() {
Base* bptr; // Base class pointer
Base B;
Derived D;
cout << "\nbptr points to Base:";
bptr = &B; // Base class object
bptr->display();
bptr->show();
cout << "\n\nbptr points to Derived:";
bptr = &D; // Derived class object
bptr->display();
bptr->show();
return 0;
}
Output:-