Oop Exp 7-17
Oop Exp 7-17
Experiment No: 7
• External Function
#include <iostream>
class Calculation
{a
private:
float num1, num2;
public:
};
if (calc.num2 != 0) {
else
int main()
float a, b;
performCalculations(calc);
return 0;
class Box
int capacity;
public:
void print();
bool compare();
};
ANS:-
#include <iostream>
#include <string>
class Box
int capacity;
public:
Box(int cap){
capacity = cap;
};
void show()
Box b(10);
cout<<"Value of capacity
is: "<<b.capacity<<endl;
show();
return 0;
Exercise:-
1. Write a C++ program to exchange the values of two variables using friend
function.
#include <iostream>
class Exchange
private:
int a, b;
public:
{
cout << "a = " << a << ", b = " << b << endl;
};
e.a = e.b;
e.b=temp;
int main()a
int x, y;
e.display();
swapValues(e);
e.display();
return 0;
}
2. WAP to create two classes test1 and test2 which stores marks of a
student. Read value for
class objects and calculate average of two tests using friend function.
#include <iostream>
class Test1;
class Test2;
class Test1
private:
float marks1;
public:
void readMarks()
};
class Test2
private:
float marks2;
public:
void readMarks()
};
int main() {
Test1 t1;
Test2 t2;
t1.readMarks();
t2.readMarks();
return 0;
}
Experiment No: 8
#include <iostream>
class WeightCalculator
private:
static float g;
float mass;
public:
WeightCalculator(float m) : mass(m) {}
g = gravity;
float calculateWeight() {
return mass * g;
cout << "Weight of the object on " << planetName << ": " <<
calculateWeight() << " N" << endl;
};
float WeightCalculator::g = 0;
int main()
float mass;
cout << "Enter the mass of the object (in kg): ";
WeightCalculator obj(mass);
WeightCalculator::setGravity(9.81);
obj.displayWeight("Earth");
WeightCalculator::setGravity(3.71);
obj.displayWeight("Mars");
WeightCalculator::setGravity(24.79);
obj.displayWeight("Jupiter");
WeightCalculator::setGravity(1.62);
obj.displayWeight("Moon");
return 0;
A) Which is correct syntax to access the static member functions with class
name?
ClassName::staticFunctionName();
C++ code?
class Test
<< “ ”;
};
void main()
Test x;
x.fun();
x.fun();
class Test
public: Test()
{
cout << "Test's
endl;
};
class Result
static Test a;
public:
Result()
cout <<
<< endl;
};
void main()
Result b;
}
Exercise:-
Write a Program to define a class having data members principal, duration
and rate of interest.
Declare rate _of_ interest as static member variable .calculate the simple
interest and display it.
#include <iostream>
class InterestCalculator
private:
double principal;
int duration;
public:
rateOfInterest = rate;
}
double calculateSimpleInterest() {
void displaySimpleInterest()
};
double InterestCalculator::rateOfInterest = 0;
int main()
InterestCalculator::setRateOfInterest(5.0);
calc.displaySimpleInterest();
return 0;
Experiment No: 9
Accept this info for object using pointer to array of object and display it.
#include <iostream>
class Birthday
private:
int day;
int month;
int year;
public:
void inputBirthday()
cout << "Invalid input. Please enter a valid day (1-31): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid month (1-12): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter year (e.g., 2024): ";
cout << "Invalid input. Please enter a valid year (>= 0): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Birthday: " << day << "/" << month << "/" << year <<
endl;
};
int main()
int n;
cin >> n;
while (n <= 0) {
cout << "Please enter a valid number of birthdays (> 0): ";
cin >> n;
{
cout << "Entering details for birthday " << (i + 1) << ":\n";
birthdays[i].inputBirthday();
birthdays[i].displayBirthday();
delete[] birthdays;
return 0;
}
If array of objects is declared as given belowwhich is the limitation on
objects?
Class_name arrayName[size];
#include<iostream>
class Employee
int id;
char name[30];
public:
void getdata();
void putdata();
};
void Employee::getdata()
void Employee::putdata()
int main()
{
Employee emp[30];
int n, i
cin >> n;
emp[i].getdata();
Balance. Accept this data for 10 accounts and display data of Accounts
having balance greater
than 10000.
#include <iostream>
class Account
private:
int Account_No;
float Balance;
public:
void getData();
void display()
float getBalance()
};
void Account::getData()
void Account::display()
cout << "Account Number: " << Account_No << ", Balance: " << Balance
<< endl;
float Account::getBalance()
{
return Balance;
int main()
Account accounts[10];
cout << "Enter details for Account " << (i + 1) << ":\n";
accounts[i].getData();
accounts[i].display();
return 0;
}
Experiment No: 10
• Parameterized Constructor
• Copy Constructor
#include <iostream>
class Calculation
private:
public:
};
if (calc.num2 != 0)
{
cout << "Division: " << (calc.num1 / calc.num2) << endl;
else
int main()
float a, b;
Calculations(calc);
return 0;
}
a) How many member functions are
class Box
int capacity;
public:
void print();
bool compare();
};
ANS:-
#include <iostream>
#include <string>
class Box
int capacity;
public:
Box(int cap){
capacity = cap;
};
void show()
Box b(10);
cout<<"Value of capacity
is: "<<b.capacity<<endl;
show();
return 0;
Exercise:-
1. Write a C++ program to exchange the values of two variables using friend
function.
#include <iostream>
class Exchange
private:
int a, b;
public:
cout << "a = " << a << ", b = " << b << endl;
};
e.a = e.b;
e.b=temp;
int main()a
int x, y;
e.display();
swapValues(e);
e.display();
return 0;
}
2. WAP to create two classes test1 and test2 which stores marks of a
student. Read value for
class objects and calculate average of two tests using friend function.
#include <iostream>
class Test1;
class Test2;
class Test1
private:
float marks1;
public:
void readMarks()
};
class Test2
private:
float marks2;
public:
void readMarks()
};
int main() {
Test1 t1;
Test2 t2;
t1.readMarks();
t2.readMarks();
Experiment No: 11
• Multilevel Inheritance
total and percentage. Accept and display data for one student.
#include <iostream>
#include <string>
protected:
int roll_no;
string name;
public:
void acceptData()
void displayData()
};
private:
float total;
float percentage;
public:
void acceptMarks()
calculateTotalAndPercentage();
void Total()
total = m1 + m2 + m3;
void Marks()
displayData();
cout << "Percentage: " << percentage << "%" << endl;
};
int main()
{
Marks student;
student.acceptData();
student.acceptMarks();
student.displayMarks();
return 0;
#include <string>
class Person
protected:
string name;
string gender;
int age;
public:
void acceptPersonData()
getline(cin, gender);
cin.ignore();
};
protected:
int employeeID;
double salary;
public:
void acceptEmployeeData()
acceptPersonData();
cin.ignore();
void displayEmployeeData() const {
displayPersonData();
};
private:
int languagesKnown;
public:
void acceptProgrammerData()
acceptEmployeeData();
cin.ignore();
displayEmployeeData();
};
int main()
{
Programmer prog;
prog.acceptProgrammerData();
prog.displayProgrammerData();
return 0;
a) #include<iostream.h>
class Base
public:
Base(){}
~Base(){}
protected:
private:
};
public:
Derived(){}
Derived(){}
private:
protected:
};
void main()
endl;
}
b) #include <iostream.h>
class A
public:
void print() {
cout <<
"print() in A";
};
class B :
private A
public:
void print() {
cout <<
"print() in B";
}
};
class C :
public B
public:
void print() {
cout <<
"print() in C";
A::print();}
};
void main()
Cb; b.print();
Exercise:-
1. WAP to implement inheritance shown below figure. Assume suitable
member function.
#include <iostream>
#include <string>
class Furniture
protected:
string material;
double price;
public:
void acceptFurnitureData()
getline(cin, material);
void displayFurnitureData()
{
};
private:
double height;
double surfaceArea;
public:
void acceptTableData()
acceptFurnitureData();
displayFurnitureData();
cout << "Height: " << height << " units" << endl;
cout << "Surface Area: " << surfaceArea << " sq. units" << endl;
};
int main()
Table myTable;
myTable.acceptTableData();
myTable.displayTableData();
return 0;
}
2. Write a C++ program to define a class "Employee" having data members
emp_no,
members basic, hra, da, gross_sal. Accept and display data for one
employee.
#include <iostream>
#include <string>
class Employee
protected:
int emp_no;
string emp_name;
string emp_designation;
public:
void acceptEmployeeData()
cin.ignore();
getline(cin, emp_name);
getline(cin, emp_designation);
}
void displayEmployeeData()
};
private:
double basic;
double hra;
double da;
double gross_sal;
public:
void acceptSalaryData()
acceptEmployeeData();
}
void displaySalaryData()
displayEmployeeData();
};
int main()
Salary emp;
emp.acceptSalaryData();
emp.displaySalaryData();
return 0;
}
Experiment No: 12
#include <iostream>
class Area
public:
{
return length * breadth;
};
class Perimeter
public:
};
private:
double length;
double breadth;
public
void getData()
void showData()
};
int main()
Rectangle rect;
rect.showData();
return 0;
}
2. Complete the following questions
Exercise:-
Identify the following type of Inheritance shown . Write a definition of each
class .Write suitablemember functions to accept and display data for each
class.
#include <iostream>
#include <string>
class Employee
protected:
int emp_id;
string emp_name;
public:
void acceptData()
cin >>emp_name;
void displayData()
};
protected:
int member_id;
public:
void acceptData()
Employee::acceptData();
cout << "Enter Union Member ID: ";
void displayData()
Employee::displayData();
cout << "Union Member ID: " << member_id << std::endl;
};
private:
double basic_salary;
public:
void acceptData()
EmpUnion::acceptData();
void displayData()
EmpUnion::displayData();
};
int main()
EmpInfo empInfo;
empInfo.acceptData();
empInfo.displayData();
return 0;
2. Write a Program to get the average marks of six subjects using the
Multiple Inheritance
#include <iostream>
class Subject1
{
protected:
float subject1_marks;
float subject2_marks;
float subject3_marks;
public:
void acceptSubject1Marks()
};
class Subject2
protected:
float subject4_marks;
float subject5_marks;
float subject6_marks;
public:
void acceptSubject2Marks()
};
public:
void calculateAverage()
};
int main()
Student student;
student.acceptSubject1Marks();
student.acceptSubject2Marks();
student.calculateAverage();
return 0;
}
Experiment No: 13
#include <string>
class Player {
protected:
std::string name;
int matches;
public:
: name(playerName), matches(playerMatches) {}
};
private:
int total_score;
float per_match_score;
public:
void calculateAverage() {
if (matches > 0) {
} else {
per_match_score = 0;
std::cout << "Average Score per Match: " << per_match_score <<
std::endl;
};
public:
: Player(playerName, playerMatches) {}
std::cout << "Bowler: " << name << " is ready to bowl!" << std::endl;
};
int main() {
return 0;
}
2.Write a C++ program to implement given figure class hierarchy.
a) #include <iostream>
using namespace std;
class Animal {
public:
void info() {
endl;
};
public:
void bark() {
};
public:
void meow() {
endl;
};
int main() {
class
Dog dog1;
class
Cat cat1;
cat1.meow();
return 0;
b)#include<iostream.h>
#include<conio.h>
class emp
int id;
char name[20];
int sal;
public:
void accept()
cin>>id>>name>>sal;
void dis()
cout<<"id"<<id;
cout<<"name--"<<name<<endl;
cout<<"salary"<<sal; }
};
public:
acc1()
accept();
dis();
};
public: acc2()
accept();
dis() ;
};
void main() {
manager m;
clerk c;
m.acc1();
c.acc2();
getch(); }
Exercise:-
#include <string>
class Employee {
protected:
string name;
int id;
float salary;
public:
void accept()
{
cout << "Enter ID: ";
getline(cin, name);
};
public:
};
public:
};
int main() {
Programmer programmer;
Manager manager;
programmer.accept();
manager.accept();
programmer.display();
manager.display();
return 0;
}
Experiment No: 14
#include <iostream>
#include <string>
class Student
protected:
string name;
int rollNumber;
public:
void displayStudentInfo() {
cout << "Name: " << name << ", Roll Number: " << rollNumber <<
endl;
}
};
protected:
public:
float totalMarks()
};
protected:
float score;
public:
float getScore()
return score;
};
public:
void displayResult()
displayStudentInfo();
cout << "Total Result: " << (totalMarks() + getScore()) << endl;
};
int main()
string name;
int rollNumber;
student.displayResult();
return 0;
return 0;
}
Complete the following
A)#include <iostream.h>
struct a
{
int count;
};
Struct b
{
int* value;
};
struct c
public a,
public b
};
int main()
{
c* p new =c;
p->value=0;
cout<< "Inherited";
return 0;
}
b)#include<iostream.h>
class A
{
public:
void display()
{
cout<< “A\n”;
};
class B : public A
{
public:
void display()
{
cout<<”B\n”;
}
};
int main()
{
B b;
b.display();
b.A::display();
b.B::display();
return 0;
}
Exercise:-
#include <iostream>
#include <string>
using namespace std;
class Cricketer
{
protected:
string name;
int age;
int matchesPlayed;
public:
Cricketer(string n, int a, int m) : name(n), age(a), matchesPlayed(m)
{}
void displayCricketerInfo()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Matches Played: " << matchesPlayed << endl;
}
};
class Bowler : virtual public Cricketer
{
protected:
int wicketsTaken;
double bowlingAverage;
public:
Bowler(string n, int a, int m, int w, double ba)
: Cricketer(n, a, m), wicketsTaken(w), bowlingAverage(ba) {}
void displayBowlerStats()
{
cout << "Wickets Taken: " << wicketsTaken << endl;
cout << "Bowling Average: " << bowlingAverage << endl;
}
};
class Batsman : virtual public Cricketer
{
protected:
int runsScored;
double battingAverage;
public:
Batsman(string n, int a, int m, int r, double ba)
: Cricketer(n, a, m), runsScored(r), battingAverage(ba) {}
void displayBatsmanStats() {
cout << "Runs Scored: " << runsScored << endl;
cout << "Batting Average: " << battingAverage << endl;
}
};
class AllRounder : public Bowler, public Batsman
{
public:
AllRounder(string n, int a, int m, int w, double baBowler, int r, double
baBatsman)
: Cricketer(n, a, m), Bowler(n, a, m, w, baBowler), Batsman(n, a,
m, r, baBatsman) {}
void displayAllRounderInfo()
{
displayCricketerInfo();
displayBowlerStats();
displayBatsmanStats();
}
};
int main()
{
string name;
int age, matchesPlayed, wicketsTaken, runsScored;
double bowlingAverage, battingAverage;
cout << "Enter Cricketer's Name: ";
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
cout << "Enter Matches Played: ";
cin >> matchesPlayed;
cout << "Enter Wickets Taken: ";
cin >> wicketsTaken;
cout << "Enter Bowling Average: ";
cin >> bowlingAverage;
cout << "Enter Runs Scored: ";
cin >> runsScored;
cout << "Enter Batting Average: ";
cin >> battingAverage;
AllRounder player(name, age, matchesPlayed, wicketsTaken,
bowlingAverage, runsScored, battingAverage);
cout << "\nPlayer Information:\n";
player.displayAllRounderInfo();
return 0;
}
#include <string>
class MasterData
protected:
string name;
int code;
public:
void displayMasterData()
};
protected:
double basicSalary;
public:
void displayAccountInfo()
};
protected:
int experience;
public:
void displayAdminInfo()
cout << "Experience: " << experience << " years" << endl;
};
class Employee : public Account, public Admin
private:
double grossSalary;
public:
calculateGrossSalary();
void calculateGrossSalary()
void displayEmployeeInfo()
displayMasterData();
displayAccountInfo();
displayAdminInfo();
};
int main()
string name;
cin>>name
emp.displayEmployeeInfo();
return 0;
}
Experiment No: 15
public:
Person(string n, int a) : name(n), age(a)
{
cout << "Person Constructor called" << endl;
}
void displayPersonIinfo()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Student : public Person
{
private:
string major;
double gpa;
public:
Student(string n, int a, string m, double g)
: Person(n, a), major(m), gpa(g)
{
cout << "Student Constructor called" << endl;
}
void displayStudentInfo()
{
displayPersonInfo();
cout << "Major: " << major << endl;
cout << "GPA: " << gpa << endl;
}
};
int main()
{
string name, major;
int age;
double gpa;
cout << "Enter Student Name: ";
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
cout << "Enter Major: ";
cin >> major
cout << "Enter GPA: ";
cin >> gpa;
Student student(name, age, major, gpa);
cout << "\nStudent Information:\n";
student.displayStudentInfo();
return 0;
}
a)class Base1
{
int data1;
public:
Base1(int i)
data1 = i;
called"<<endl;
void printDataBase1()
"<<data1<<endl;
};
class Base2
int data2;
public:
Base2(int i)
data2 = i;
void printDataBase2(void){
};
public:
Base2(b), Base1
derived1 = c;
derived2 = d;
called"<<endl;
void printDataDerived(void)
};
b) #include <iostream>
class A
{
public:
A(int n )
cout << n;
};
class B: public A
public:
B(int n, double d)
: A(n)
cout << d;
};
class C: public B
public:
:B(n, d)
cout <<ch;
};
int main()
{
C c(5, 4.3, 'R');
return 0;
Exercise:-
#include <iostream>
class Parent
public:
Parent()
{
};
public:
Child()
};
int main()
Child obj;
return 0;
2. #include <iostream>
using namespace std;
class Parent1
public:
Parent1()
};
class Parent2
public:
Parent2()
};
public:
Child()
};
int main()
{
Child obj1;
return 0;
Experiment No: 16
▪ Pointer to object
▪ 'this' pointer
#include <iostream>
#include <string>
using namespace std;
class Book
private:
string book_name;
string author_name;
double price;
public:
void inputDetails()
cin.ignore();
getline(cin, book_name);
getline(cin, author_name);
};
int main()
{
Book* bookPtr = new Book();
bookPtr->inputDetails();
bookPtr->displayDetails();
delete bookPtr;
return 0;
width and breadth. Accept this information for one object using pointer to
class Box
private:
double height;
double width;
double breadth;
public:
void inputDetails()
}
};
int main() {
boxPtr->inputDetails();
boxPtr->displayAreaAndVolume();
delete boxPtr;
return 0;
year. Accept this information for five objects using pointer to the array of
objects
#include <iostream>
private:
int day;
int month;
int year;
public:
void inputDetails()
void displayDetails()
cout << day << "/" << month << "/" << year << endl;
};
int main()
cout << "Enter details for Birthday " << (i + 1) << ":\n";
birthdays[i].inputDetails();
birthdays[i].displayDetails();
delete[] birthdays;
return 0;
}
Complete the following:
a) #include <iostream>
#include <iostream>
class Employee
public:
int id;
string name;
float salary;
this->id = id;
this->name = name;
this->salary = salary;
void display()
};
int main
an object of Employee
e1.display();
e2.display();
return 0;
}
B) #include <iostream>
#include <string>
class student
private:
int rollno;
string name;
public:
student():rollno(0),name(1111)
{}
{}
void get()
cin>>name;
void print()
cout<<"roll no is 11<<rollno;
cout<<"name is "<<name;
};
void main ()
Exercise:-
#include <iostream.h>
#include<conio.h>
class myclass
int i;
public:
void read(int j)
i= j;
}
int getint()
return i;
};
void main()
clrscr();
objectPointer = &ob;
objectPointer->read(lO);
cout<<objectPointer->getint();
getch();
2. Which is the pointer which denotes the object calling the member
function?
a) Null b) zero
#include <iostream>
class Rectangle
private:
double length;
double width;
public:
this->length = length;
this->width = width;
double area()
}
void display()
};
int main()
rect.display();
return 0;
Experiment No: 17
and height. Derive classes "rectangle" and "triangle" from "polygon" having
#include <iostream>
class Polygon
protected:
double width;
double height;
public:
};
public:
double area()
{
return width * height;
};
public:
double area()
};
int main()
Polygon p;
p = ▭
p = &tri;
Write a program which show the use of Pointer to derived class in multilevel
Inheritance.
#include <iostream>
class Animal
public:
void sound()
};
public:
void walk()
};
public:
void bark()
};
int main() {
dogPtr->sound();
dogPtr->walk();
dogPtr->bark();
delete dogPtr;
return 0;
}
3.Complete the following:-
a) #include<iostream.h>
class base
public:
int nl;
void show()
cout<<"\nnl"<<nl;
};
public:
int n2;
void show()
cout<<"\nnl "<<nl;
cout<<"\nn2 "<<n2;
};
int main()
base b;
base *bptr;
bptr=&b;
bptr->n1=44;
bptr->show();
derived d;
cout<<"\n";
bptr=&d;
bptr->n1=66;
bptr->show();
return 0;
b) #include <iostream.h>
class BaseClass
int x;
public:
void setx(int i)
X = i;
int getx()
return x;
};
int y;
public:
void sety(int i)
y = i;
int gety()
return y;
};
int main()
BaseClass *p;
BaseClass baseObject;
DerivedClass derivedObject;
p = &baseObject;
p->setx(l0);
p = &derivedObject;
p->setx(99);
derivedObject.sety(88);
return 0;
Exercise:-
#include<iostream.h>
class base
public:
void show()
};
class derived: public base
public:
void show()
};
void main()
base bl;
bl.show();
derived dl;
dl.show();
pb->show();
pb = &dl;
pb->show();
}
2. A pointer to the base class can hold address of
C) base class object as well as derived class object D) None of the above
Ans:-
a) Null b) zero
Ans:-
d) All of them
A) Reference B) Pointer
Ans:-
B) Pointer
Experiment No: 18
#include <iostream>
using namespace std;
int temp = a;
a = b;
b = temp;
float temp = a;
a = b;
b = temp;
char temp = a;
a = b;
b = temp;
int main()
cout << "Before interchanging: int1 = " << int1 << ", int2 = " << int2
<< endl;
interchange(int1, int2);
cout << "After interchanging: int1 = " << int1 << ", int2 = " <<int2 <<
endl;
float float1 = 5.5, float2 = 10.5;
cout << "Before interchanging: float1 = " << float1 << ", float2 = " <<
float2 << endl;
interchange(float1, float2);
cout << "After interchanging: float1 = " << float1 << ", float2 = " <<
float2 << endl;
cout << "Before interchanging: char1 = " << char1 << ", char2 = " <<
char2 << endl;
interchange(char1, char2);
cout << "After interchanging: char1 = " << char1 << ", char2 = " <<
char2 << endl;
return 0;
}
2.Write a C++ Program that find the sum of two int and double number
#include <iostream>
return a + b;
return a + b;
int main()
return 0;
}
3. Write C++ program to find the area of various geometrical shapes by
#include <iostream>
return 2 * Pi * radius;
int main()
cout << "Area of Triangle: " << area(base, height, true)<< endl;
return 0;
}
4. Complete the following table:
a) #include<iostream.h>
int absolute(int);
float absolute(float);
int main()
int a= -5;
float b = 5.5;
<< endl;
return 0;
if (var < 0)
{
var= -var;
return var;
if (var< 0.0)
var= -var;
return var;
b) #include <iostream.h>
class Test
public:
int main(int s)
{
return 0;
return 0;
return 0;
};
int main()
Test obj;
obj.main(3);
obj.main(9, 6);
return 0;
}
Exercise:
1.Does constructor overloading isimplementation of function overloading?
#include <iostream>
class Box
public:
Box()
Box(int length)
cout << "Constructor with one parameter called: " << length << endl;
{
cout << "Constructor with two parameters called: " << length << ", "
<< width << endl;
};
int main()
Box box1;
Box box2(5);
return 0;
#include<iostream.h> #include<conio.h>
return (a * b);
return (a / b);
int main()
m = 2.0;
return 0;
}
3. Which of the following in Object Oriented Programming is supported
by Function
overloading and default arguments features of C++?
a) Inheritance b) Polymorphism
c) Encapsulation d) None of these
ANS:-
b) Polymorphism
Experiment No: 19
• Friend function