Practical No
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;
}
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;}
};
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 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;
}
// 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();
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
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
}
int main() {
xyz a1; // Object a1 of class xyz defined
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
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
};
void integer::display() {
cout << "\nValue of m is: " << m;
cout << "\nValue of n is: " << n;
}
int main() {
integer int1; // Object int1 of class integer
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);
}
int main() {
dimension d; // Object of class dimension
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
};
int main() {
space m;
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
};
int main() {
space m;
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;
Output:-