Oopm First 4 Pages
Oopm First 4 Pages
Acropolis Institute of
Technology and
Research, Indore Department of CSE
Submitted To: Prof. Sumit Jain
(Artificial Intelligence & Machine
Learning)
Submitted By:
Name of Student
Enrollment No. : 0827
Class/Year/Sem : ALS1/2nd / 3rd
CERTIFICATE
This is to certify that the experimental work entered in this journal as per the B.
TECH. II year syllabus prescribed by the RGPV was done by Mr./ Ms.
This lab is dedicated for students to work on practical experiments, projects and research
work related to courses such as object oriented programing. This lab purpose is to enhance
the programming skills of the students by giving practical assignments and also requisite
knowledge about Object Oriented Programming through C++ so that they make their own
Applications/Projects using C++.
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES
DO’S
While entering into the LAB students should wear their ID cards.
Students should sign in the LOGIN REGISTER before entering into the
laboratory.
Students should come with observation and record note book to the laboratory.
After completing the laboratory exercise, make sure to shutdown the system
properly.
DONT’S
Module2: Encapsulation and Data Abstraction- Concept of Objects: State, Behavior &
Identity of an object; Classes: identifying classes and candidates for Classes Attributes
and Services, Access modifiers, Static members of a Class, Instances, Message passing,
and Construction and destruction of Objects.
PREREQUISITE: -
Course Outcomes
At the end of the course student will be able to:
Additional remarks
Tutor
1 Title
2 Neatly Drawn and labeled experimental setup
3 Theoretical solution of the instant problem
3.1 Algorithm
3.2 Program
4 Tabulation Sheet
5
INPUT OUTPUT
Results
Page 11
Q1. Write a program in C++ which uses functions to swap two integer & two float numbers by
using reference variable.
#include <iostream>
using namespace std;
int main() {
int int1, int2;
float float1, float2;
cout << "\nBefore swapping integers: " << int1 << " " << int2;
swap(int1, int2);
cout << "\nAfter swapping integers: " << int1 << " " << int2;
cout << "\n\nBefore swapping floats: " << float1 << " " << float2;
swap(float1, float2);
cout << "\nAfter swapping floats: " << float1 << " " << float2;
return 0;
}
Page 12
Q2. Create a single program to perform following tasks without using library functions:
• To reverse the string accepted as an argument.
• To count the number of characters in string passed as argument in form of character array.
#include <iostream>
using namespace std;
int main(){
char input[100];
reverseString(input);
cout << "reversed string: " << input << endl;
return 0;
}
Page 13
Q3. Create a class Student having data members to store roll number, name of student, name of three
subjects, max marks, min marks, obtained marks. Declared an object of class student. Provide facilities to
input data in data members and display result of student.
#include <iostream>
using namespace std;
class Student {
private:
int rollNumber;
string name;
string subjects[3];
int maxMarks[3];
int minMarks[3];
int obtainedMarks[3];
public:
void inputDetails(){
cout << "enter roll number: "<< endl;
cin >> rollNumber;
cout << "Enter maximum marks for " << subjects[i] << ": ";
cin >> maxMarks[i];
cout << "Enter minimum marks required for " << subjects[i] << ": ";
cin >> minMarks[i];
cout << "Enter marks obtained in " << subjects[i] << ": ";
cin >> obtainedMarks[i];
cin.ignore();
}
}
Page 14
void displayResult() {
cout << "\n--- Student Details ---" << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Name: " << name << endl;
int totalMarks = 0;
int totalObtained = 0;
bool passed = true;
totalMarks += maxMarks[i];
totalObtained += obtainedMarks[i];
int main() {
Student student;
student.inputDetails();
student.displayResult();
return 0;
}
Page 15
Q4. Create a class student having data members to store roll no, name of student, name of 3 subjects, max
marks, min marks, obtain marks. use nesting of member function Declare an array of object to input data
of 3 students. Provide facilities to display result of all students and to display result of specific student
whose roll number is given?
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
char name[50];
char subjects[3][50];
int maxMarks[3];
int minMarks[3];
int obtainedMarks[3];
public:
void inputData() {
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Maximum Marks for " << subjects[i] << ": ";
cin >> maxMarks[i];
cout << "Enter Minimum Marks for " << subjects[i] << ": ";
cin >> minMarks[i];
cout << "Enter Obtained Marks for " << subjects[i] << ": ";
Page 16
cin >> obtainedMarks[i];
}
}
int main() {
const int numStudents = 3;
Student students[numStudents];
int searchRollNo;
cout << "\nEnter Roll Number to search: ";
cin >> searchRollNo;
Page 17
found = true;
break;
}
}
if (!found) {
cout << "\nStudent with Roll Number " << searchRollNo << " not found.\n";
}
return 0;
}
Page 18
Q5: Create a class AIML and also create a parameterized constructor AIML(int t) and a copy constructor
AIML(AIML &m) and compute a program to copy data of one object to another object.
#include <iostream>
using namespace std;
class AIML{
private:
int data;
public:
AIML(int d){
data = d;
cout <<"parameterized constructor called. data :"<< data << endl;
}
AIML(const AIML &m){
data = m.data;
cout <<"Copy constructor called. Copied Data: " << data << endl;
}
void display() const{
cout << "data: "<<data << endl;
}
};
int main(){
AIML std1(44);
AIML std2 = std1;
cout << "\nStudent 1 :";
std1.display();
cout<< "\nStudent 2 :";
std2.display();
return 0;
}
Page 19
Q6: Create a class student. The student class has data members such as roll number, name of student,
contact number and address. create the derived class test which contains data members representing name
of subject, and test marks of 5 subjects. Display all the information of student.
#include <iostream>
#include <string>
using namespace std;
class Student {
protected:
int rollNumber;
string name;
string contactNumber;
string address;
public:
void displayStudentInfo() {
cout << "Roll Number: " << rollNumber << endl;
cout << "Name: " << name << endl;
cout << "Contact Number: " << contactNumber << endl;
cout << "Address: " << address << endl;
}
};
Page 20
int marks[5];
public:
void displayInfo() {
displayStudentInfo();
cout << "Subjects and Marks:" << endl;
for (int i = 0; i < 5; i++) {
cout << subjects[i] << ": " << marks[i] << endl;
}
}
};
int main() {
string subjects[] = {"Math", "Physics", "Chemistry", "English", "Computer"};
int marks[] = {85, 90, 78, 88, 95};
student1.displayInfo();
return 0;
}
Page 21
Q7: Consider an example of declaring the examination result. Design 3 classes student, exam, result. The
student class has data members such as that representing number, name of student ,create the class exam,
which contains data members representing name of subject, minimum marks, maximum marks, obtained
marks for 3 subject derive class result from both student and exam classes.
#include <iostream>
using namespace std;
class Student {
protected:
int studentNumber;
char name[50];
public:
int i = 0;
while (studentName[i] != '\0' && i < 49) {
name[i] = studentName[i];
i++;
}
name[i] = '\0';
}
void displayStudentInfo() {
cout << "Student Number: " << studentNumber << endl;
cout << "Name: " << name << endl;
}
};
Page 22
class Exam {
protected:
char subjects[3][20];
int maxMarks[3];
int obtainedMarks[3];
public:
void setExamDetails(const char subNames[3][20], const int max[], const int obtained[]) {
for (int i = 0; i < 3; i++) {
int j = 0;
while (subNames[i][j] != '\0' && j < 19) {
subjects[i][j] = subNames[i][j];
j++;
}
subjects[i][j] = '\0';
maxMarks[i] = max[i];
obtainedMarks[i] = obtained[i];
}
}
void displayExamDetails() {
for (int i = 0; i < 3; i++) {
cout << "Subject: " << subjects[i]
<< ", Max Marks: " << maxMarks[i]
<< ", Obtained Marks: " << obtainedMarks[i] << endl;
}
}
};
Page 23
cout << "Percentage: " << (totalObtained * 100.0 / totalMarks) << "%\n";
}
};
int main() {
Result result;
result.setStudentDetails(11, "Aditya");
result.displayResult();
return 0;
}
Q8: Create class Polar having data member’s radius and angle. It contains member functions for taking
input in data member function for displaying value of data members. Class Polar contains declaration of
friend function add which accepts two objects of class Polar and returns objects of class Polar after
addition. Test the class using main function and object of class Polar.
#include <iostream>
#include <cmath>
using namespace std;
class Polar {
private:
double radius;
double angle;
public:
Page 24
Polar(double r, double a) : radius(r), angle(a) {}
void input() {
cout << "Enter radius: ";
cin >> radius;
cout << "Enter angle (in degrees): ";
double angleDegrees;
cin >> angleDegrees;
angle = angleDegrees * M_PI / 180.0;
}
double x = x1 + x2;
double y = y1 + y2;
int main() {
Polar p1, p2;
cout << "Enter details for the first polar coordinate:" << endl;
p1.input();
cout << "Enter details for the second polar coordinate:" << endl;
p2.input();
Page 25
cout << "\nSecond Polar Coordinate:" << endl;
p2.display();
return 0;
}
Q9: Create a class Static_demo with static member functions for following tasks: -
• To find factorial by recursive member function
• To check whether a no. is prime or not.
#include <iostream>
using namespace std;
class Static_demo {
public:
Page 26
}
return n * factorial(n - 1);
}
int main() {
int num;
return 0;
}
Q10: Create a C++ class number with integer data member. Write necessary member function to overload the
operator unary pre and post increment “++”.
#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Page 27
Number(int v = 0) : value(v) {}
Number& operator++() {
++value;
return *this;
}
Number operator++(int) {
Number temp = *this;
value++;
return temp;
}
};
int main() {
Number num(5);
return 0;
}
Q11: Implement polymorphism using inheritance and virtual functions to calculate the area of different shapes
(Circle, Rectangle, and Triangle).
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
Page 28
virtual void calculateArea() const {
cout << "This is a generic shape. Area calculation not implemented." << endl;
}
virtual ~Shape() {}
};
public:
Circle(double r) : radius(r) {}
public:
public:
Page 29
};
int main() {
circle->calculateArea();
rectangle->calculateArea();
triangle->calculateArea();
}
Q12: Implement an abstract class Employee that defines a structure for different types of employees and
calculates their salaries using derived classes.
#include <iostream>
using namespace std;
Page 30
class Employee {
protected:
int id_number;
public:
Employee(int id) : id_number(id) {}
virtual ~Employee() {}
};
public:
SalariedEmployee(int id, double salary) : Employee(id), monthlySalary(salary) {}
public:
HourlyEmployee(int id, double rate, double hours)
: Employee(id), hourlyRate(rate), hoursWorked(hours) {}
Page 31
public:
CommissionEmployee(int id, double salesAmount, double rate)
: Employee(id), sales(salesAmount), commissionRate(rate) {}
int main() {
SalariedEmployee salaried(1, 5000);
HourlyEmployee hourly(2, 20, 160);
CommissionEmployee commission(3, 10000, 0.1);
salaried.getDetails();
cout << "Salary: $" << salaried.calculateSalary() << endl;
hourly.getDetails();
cout << "Salary: $" << hourly.calculateSalary() << endl;
commission.getDetails();
cout << "Salary: $" << commission.calculateSalary() << endl;
return 0;
}
Page 32
Q13: Write a program to implement virtual function using a base class A & derived class B and also create
two member functions show(), display() which will display the name of class and override them in
derived class making show as virtual function.
#include <iostream>
using namespace std;
class A {
public:
virtual ~A() {}
};
class B : public A {
public:
Page 33
int main() {
A* basePtr;
B derivedObj;
basePtr = &derivedObj;
cout << "Calling functions through base class pointer:" << endl;
basePtr->show();
basePtr->display();
cout << "\nCalling functions directly through derived class object:" << endl;
derivedObj.show();
derivedObj.display();
return 0;
}
Q14: Create a class called dimension containing three float data and a constructor to accept values. Also
declare a pure virtual function area () in it. Create three derived classes Rectangle, Square and Triangle
each inheriting dimension as public. Define corresponding constructors and redefine virtual function
area in each. Write a complete program and also print the version of virtual function used each time
area is printed.
#include <iostream>
#include <string>
using namespace std;
class Dimension {
protected:
float a, b, c;
public:
virtual ~Dimension() {}
Page 34
public:
int main() {
Page 35
Dimension* triangle = new Triangle(6.0f, 8.0f);
delete rectangle;
delete square;
delete triangle;
return 0;
}
Q15: Write a program to accept the student detail such as name and 3 different marks by get_data()
method and display the name and average of marks using display() method. Define a friend class for
calculating the average of marks using the method mark_avg().
#include <iostream>
#include <string>
using namespace std;
class MarkAvg;
class Student {
private:
string name;
float marks[3];
public:
void get_data() {
cout << "Enter student name: ";
cin >> name;
cout << "Enter 3 marks: ";
for (int i = 0; i < 3; i++) {
cin >> marks[i];
}
Page 36
}
void display() {
cout << "Student Name: " << name << endl;
MarkAvg m;
cout << "Average Marks: " << m.mark_avg(*this) << endl;
}
class MarkAvg {
public:
int main() {
Student student;
student.get_data();
student.display();
return 0;
}
Page 37