AMITY UNIVERSITY RAJASTHAN
Amity School of Engineering &
Technology
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
Practical Lab Record of
BCS324/ Object Oriented Programming using C++
Submitted by:
Submitted to:
Student Name: SARTHAK GUPTA
Prof. (Dr.) Sunil Pathak
Enrollment No.: A20405222222
Section: D
BCS324/ Object Oriented Programming using
C++
Lab Assignments
Note: Don’t copy and paste the assignment from internet source. All students need to
submit practical record hand written / print out. Assignment must be unique for each
student.
•Write a C++ Program to display Names, Roll No., and grades of 3 students who have
appeared in the examination. Declare the class of name, Roll No. and grade. Create an array
of class objects. Read and display the contents of the array.
•Write a C++ program to declare Struct. Initialize and display contents of member
variables.
•Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.
•Given that an EMPLOYEE class contains following members: data members:
Employee number, Employee name, Basic, DA, IT, Net Salary and print data
members.
•Write a C++ program to read the data of N employee and compute Net salary of each
employee (DA=52% of Basic and Income Tax (IT) =30% of the gross salary).
•Write a C++ to illustrate the concepts of console I/O operations.
•Write a C++ program to use scope resolution operator. Display the various values of the
same variables declared at different scope levels.
•Write a C++ program to allocate memory using new operator.
•Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
•Write a C++ program to create an array of pointers. Invoke functions using array
objects.
•Write a C++ program to use pointer for both base and derived classes and call the
member function. Use Virtual keyword.
•Write a C++ Program to swap the value of two numbers using templates.
•Write a C++ Program to overload + and ++ operator.
•Write a C++ program to allocate memory using new operator.
•Write a C++ program to use pointer for both base and derived classes and call the
member function. Use Virtual keyword.
•Write a C++ Program to display Names, Roll No., and grades of 3 students who have
appeared in the examination. Declare the class of name, Roll No., and grade. Create an array
of class objects. Read and display the contents of the array.
#include
<iostream> OUTPUT:
#include <string>
Enter details for student 1: Enter
using namespace name: SARTHAK
std; Enter RollNo:10 Enter
Grade: B
Enter details for student 2: Enter
class Student name: SUBHASH
Enter RollNo: 12 Enter
{ public: Grade: A
string
name; int
rollNo;
char grade;
void
inputStudentDetails()
{ cout << "Enter name:
"; getline(cin, name);
cout << "Enter Roll
No.: "; cin >> rollNo;
cout << "Enter grade:
"; cin >> grade;
[Link]();
}
void displayStudentDetails() {
cout << "Name: " << name <<
endl; cout << "Roll No.: " <<
rollNo << endl; cout << "Grade: "
<< grade << endl; cout << endl;
};
int main() {
const int numStudents = 3;
Student students[numStudents];
for (int i = 0; i < numStudents; ++i) {
cout << "Enter details for student " << i + 1 << ":\n";
students[i].inputStudentDetails();
}
cout << "Student Details:\n";
for (int i = 0; i < numStudents; ++i) {
cout << "Details for student " << i + 1 << ":\n";
students[i].displayStudentDetails();
}
return 0;
}
2) Write a C++ Program to declare struct . Initialise and display contents of
member variables
•OUTPUT:
#include Student Information:
<iostream> using Name: SARTHAK GUPTA
namespace std;
RollNo: 100
struct
Student Grade: A
{ string
name; int
rollNo;
char grade;
};
int main() {
Student student1;
[Link] = “SA”RTHAK
GUPTA; [Link] = 100;
[Link] = 'A';
cout << "Student Information:" << endl;
cout << "Name: " << [Link] <<
endl; cout << "Roll No.: " <<
[Link] << endl; cout << "Grade: "
<< [Link] << endl;
return 0;
}
•Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member
OUTPUT:
Person Information:
Name: RANBIR
Age: 30
Gender: M
#include
<iostream>#include
<string>
using namespace std;
class Person
{ public:
string
name; int
age;
char gender;
};
int main() {
Person* personPtr = new Person;
personPtr->name =
“RANBIR”; personPtr-
>age = 30; personPtr-
>gender = 'M';
cout << "Person Information:"<< endl;
cout << "Name: " << personPtr->name <<
endl; cout << "Age: " << personPtr->age <<
endl;
cout << "Gender: " << personPtr->gender << endl;
delete personPtr;
return 0;
}
•Given that an EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary and print data members.
OUTPUT:
Employee Information:
Employee Number: 50
Employee Name: TONY STARK
Basic Salary: 60000
DA (Dearness Allowance):15000IT
(Income Tax): 5000
Net Salary: 55000
#include
<iostream>
#include <string>
using namespace
std;
c l a s s
Employee
{ public:
i n t
employeeNumber;
s t r i n g
employeeName;
double basic;
double
da; double
it;
double netSalary;
void
calculateNetSalary()
{ netSalary = basic +
da - it;
}
void displayEmployeeDetails() {
std::cout << "Employee Information:"<<endl;
std::cout << "Employee Number: " << employeeNumber <<
endl; std::cout << "Employee Name: " << employeeName
<< endl; std::cout << "Basic Salary: " << basic << endl;
std::cout << "DA (Dearness Allowance): " << da <<
endl; std::cout << "IT (Income Tax): " << it << endl;
std::cout << "Net Salary: " << netSalary << endl;
}
};
int main() {
Employee employee;
[Link] = 101;
[Link] = “TONY
STARK”; [Link] = 60000.0;
[Link] = 15000.0;
[Link] = 5000.0;
[Link]();
[Link]();
return 0;
}
•Write a C++ program to read the data of N employee and compute Net salary of each employee (DA=52%
of Basic and Income Tax (IT) =30% of the gross salary
#include OUTPUT:
<iostream>#include
Enter the number of Employees:2
<string> Enter details for employee 1:
using namespace std; Name: SARTHAK
Basic Salary: 200000
Enter details for employee 2:
class Employee Name: SUBHASH
{public: Basic Salary: 250000
Employee Information:
int employeeNumber;
Employee Number: 1
s t r i n g Employee Name:
employeeName; SARTHAK Basic
double basic; Salary: 200000
double DA (Dearness Allowance): 52000
da; double Income Tax: 45600
Gross Salary: 152000
it;
double
grossSalary; double
netSalary;
void calculateSalary()
{ da = 0.52 * basic;
it = 0.30 * (basic + da);
grossSalary = basic +
da;
netSalary = grossSalary - it;
void inputEmployeeDetails(int number)
{ employeeNumber = number;
cout << "Enter details for employee " << employeeNumber << ":" << endl;
cout << "Name: ";
[Link]();
getline(cin, employeeName);
cout << "Basic Salary:
"; cin >> basic;
calculateSalary();
void displayEmployeeDetails() {
cout << "Employee Information:" << endl;
cout << "Employee Number: " << employeeNumber <<
endl; cout << "Employee Name: " << employeeName <<
endl; cout << "Basic Salary: " << basic << endl;
cout << "DA (Dearness Allowance): " << da << endl;
cout << "Income Tax: " << it << endl;
cout << "Gross Salary: " << grossSalary << endl;
cout << "Net Salary: " << netSalary << endl <<
endl;
}
};
int main()
{ int N;
cout << "Enter the number of employees: ";
cin >> N;
Employee
employees[N]; for (int i =
0; i < N; ++i) {
employees[i].inputEmployeeDetails(i + 1);
for (int i = 0; i < N; ++i) {
employees[i].displayEmployeeDetails();
return 0;
}
6) write a c++ program to illustrate the concepts of console I/O operations.
•OUTPUT:
Enter your Name:
#include <iostream>
HARSH
using namespace std;
Enter your Age:
int
main() 21
{int age;
double Enter your height(in feet): 6’1
height; string Hello, harsh!
name;
You are 21 years old.
cout << "Enter your name: Your height is 6’1 ft.
"<<endl; getline(cin, name);
cout << "Enter your age:
"<<endl; cin >> age;
cout << "Enter your height (in meters): "<<endl;
cin >> height;
cout << endl;
cout << "Hello, " << name << "!" << endl;
cout << "You are " << age << " years old." << endl;
cout << "Your height is " << height << " meters." << endl;
return 0;
}
•Write a C++ program to use scope resolution operator. Display the various values of the
same variables declared at different scope levels.
OUTPUT:
Global variable:10
Local variable in main function: 20 Local
variable in nested block: 30 Global
variable:10
Local variable in main function: 20
#include
<iostream> using
namespace std;
int globalVar =
10; int main() {
int localVarMain = 20;
{
int localVarNestedBlock = 30;
cout << "Global variable: " <<::globalVar << endl;
cout << "Local variable in main function: " << localVarMain << endl;
cout << "Local variable in nested block: " << localVarNestedBlock << endl;
}
cout << "Global variable: " << ::globalVar << endl;
cout << "Local variable in main function: " << localVarMain << endl;
return 0;
}
8)write a c++ program to allocate memory using new operator.
•
OUTPUT:
Dynamically allocated integer value: 42
Address of the dynamically allocated integer:
ADDRESS
#include <iostream> using namespace std;
int main() {
int* dynamicInt = new int;
*dynamicInt = 42;
cout << "Dynamically allocated integer value: " << *dynamicInt << endl;
cout << "Address of the dynamically allocated integer: " << dynamicInt << endl;
delete dynamicInt;
return 0;
}
9) write a c++ program to create a multilevel inheritance
•
OUTPUT:
Animal is eating. Mammal is running.
Dog is barking.
Code:
#include <iostream>
using namespace std;
class animal
{ public:
void eat() {
cout << “Animal is eating.” << endl;
};
class Mammal : public
student{ public:
void run() {
cout << “Mammal is running.” << endl;
};
class dog : public { public:
void bark() {
cout << "Dog is barking." << endl;
};
int main()
{ Dog
myDog;
[Link]()
;
[Link]();
[Link]();
return 0;
10) write a c++ program to create an array of pointers . Invoke functions using array objects.
•OUTPUT:
Information for element 1: Value:
30
Address: ADDRESS
Information for element 2: Value:
40
Address: ADDRESS
Information for element 3: Value:
50
Address: ADDRESS
code:
#include
<iostream> using
namespace std;
void displayInfo(int* pointer) {
cout << "Value: " << *pointer <<
endl; cout << "Address: " <<
pointer << endl;
}
int main() {
const int size = 3;
int array[size] = {30, 40,
50}; int* pointers[size];
for (int i = 0; i < size;
++i) { pointers[i] =
&array[i];
}
for (int i = 0; i < size; ++i) {
cout << "Information for element " << i + 1 << ":" << endl;
displayInfo(pointers[i]);
cout << endl;
return 0;
}
•Write a C++ program to use pointer for both base and derived classes and call the member
function. Use Virtual keyword.
OUTPUT
Using pointer to Shape:
Drawing a Shape
CODE: Using pointer to Circle (derived from Shape): Drawing a Circlec
#include<iostream>
Using namespace std ;
class Shape
{ public:
virtual void draw() {
cout << "Drawing a Shape" << endl;
} };
class Circle : public Shape
{ public:
void draw() override {
cout << "Drawing a Circle" << endl;
} };
int main() {
Shape shapeObj;
Circle circleObj;
Shape* shapePtr =
&shapeObj; Shape* circlePtr =
&circleObj;
cout << "Using pointer to Shape:" <<
endl; shapePtr->draw();
cout << "Using pointer to Circle (derived from Shape):" <<
endl; circlePtr->draw();
return 0;
•Write a C++ Program to swap the value of two numbers using templates.
OUTPUT:
Before swapping integers: num1: 10
num2: 20
After swapping integers: num1: 20
num2: 10
CODE:
#INCLUDE<IOSTREAM>
USING NAMESPACE STD;
template <typename T>
void swapValues(T& a,
T& b) { T temp = a;
a = b;
b = temp;
}
int main() {
int num1 = 10, num2 = 20;
cout << "Before swapping integers:" <<
endl; cout << "num1: " << num1 <<
endl;
cout << "num2: " << num2 << endl;
swapValues(num1, num2);
cout << "After swapping integers:" <<
endl; cout << "num1: " << num1 <<
endl;
cout << "num2: " << num2 << endl;
return 0;
}
13)Write a c++ program to overload + and ++ operator.
•OUTPUT:
#include <iostream> Count for c1: 1 Count for c2: 1
using namespace std; Count after addition: 2
class Counter
{ private:
int count;
public:
Counter() : count(0) {}
Counter operator+(const Counter& other)
{ Counter result;
[Link] = this->count + [Link];
return result;
}
Counter operator++
(int) { Counter temp =
*this; this->count++;
return temp;
int getCount()
const { return
count;
}
};
int main() {
Counter c1, c2,
result; c1++;
cout << "Count for c1: " << [Link]() << endl; c2+
+;
cout << "Count for c2: " << [Link]() << endl;
result = c1 + c2;
cout << "Count after addition: " << [Link]() << endl;
return 0;
}