ANURAG CPP Lab Manual A1to10
ANURAG CPP Lab Manual A1to10
AIM : WAP in C++ to create a class called as student. Data members are rollno,
name & fees of the student. Write appropriate get () & put () functions to scan
and display the student data.
Theoretical Explanation:
This C++ program demonstrates the creation of a `student` class and the
implementation of two member functions, `get` and `put`, to input and display
student data, respectively. Here's how the program works:
`student` Class : A class named `student` is defined with three private data
members: `rollno` (integer), `name` (string), and `fees` (floating-point number).
These data members represent the roll number, name, and fees of a student.
`get` Member Function : The `get` function is a public member of the `student`
class. It is responsible for collecting input data for a student object. It displays
prompts for the user to input the roll number, name, and fees. The entered
values are then stored in the corresponding data members of the class.
`put` Member Function : The `put` function is another public member of the
`student` class. It is used to display the student's data. It displays the roll
number, name, and fees of the student on the console.
1. Start
- `rollno` (integer)
- `name` (string)
- Display "Enter Roll Number:" and read the value into `rollno`.
- Call the `get` function to input student data (roll number, name, and fees).
7. End
Source Code:
#include<iostream>
#include<string>
class student {
string name;
float fees;
void put() {
cout << "Student Roll Number : " << rollno << endl;
};
int main() {
student ans;
ans.get();
ans.put();
return 0;
Output :
ASSIGNMENT 2
AIM: WAP in C++ to create a class called as employee. Data members are eid,
sal& name of the employee. Scan the data for 10 such employees & display the
same by using array of objects.
Theoretical Explanation:
`Employee` Class : The `Employee` class contains three data members - `cid` for
Employee ID (integer), `sal` for Salary (double), and `name` for the Employee
Name (string).
Member Functions :
- `setData(int c, double s, string n)`: This function sets the data for an
employee object by accepting the Employee ID, Salary, and Name as
arguments. It assigns these values to the respective data members of the
object.
`main` Function :
- Using a `for` loop, the program collects data for each employee. For each
employee:
- Variables `id`, `salary`, and `empName` are declared to store the ID, salary,
and name.
- The program prompts the user to input these details, and `setData` is
called to store the data in the appropriate `Employee` object within the
`employees` array.
- Another `for` loop is used to display the data for all 10 employees:
- The `displayData` function is used to display the employee's ID, Name, and
Salary.
Conclusion :
In conclusion, this program exemplifies the use of classes and arrays of objects
in C++. It efficiently collects and displays employee data, promoting structured
and organized data management, which is essential for applications that
handle large datasets of employee information.
Algorithm:
1. Start
2. Create a class `Employee` with data members:
- Declare variables `id` (for ID), `salary` (for salary), and `empName`
( employee's name).
- Prompt the user to input the details for each employee, including ID,
name, and salary.
- Use another `for` loop to display the data for all 10 employees:
- Use the `displayData` function to display the employee's ID, name, and
salary.
Source Code :
#include <iostream>
#include <string>
class Employee {
public:
int cid;
double sal;
string name;
cid = c;
sal = s;
name = n;
void displayData() {
};
int main() {
Employee employees[10]; // Array of Employee objects
int id;
double salary;
string empName;
cout << "\n" << "Enter Details [ Employee " << i + 1 << " ]" << "\n";
getline(cin, empName);
}
cout <<"\n"<< "~----Display All Employee Details----~" << "\n";
cout << "\n" << " [ Employee " << i + 1 << " ]" << "\n";
employees[i].displayData();
return 0;
Output :
ASSIGNMENT 3
AIM: WAP in C++ to create a class called as Book. Data members are name of
the Book & price. Write default, parameterized & copy constructors to initialize
& display Book object values.
Theoretical Explanation:
`Book` Class : The `Book` class contains two private data members - `name` and
`price`. These data members represent the name and price of a book.
Constructors :
Display Function (`display`) : This function displays the name and price of the
book using the `cout` statements.
`main` Function :
- The program starts by displaying a message indicating the beginning of the
program.
Conclusion :
Algorithm:
1. Start
- Default Constructor:
- Sets the `name` to "No Title" and `price` to 0.0 as default values.
- Parameterized Constructor:
- Create a third instance of the `Book` class using the copy constructor,
initializing it with the values from the second book.
- Display the values of the third book using the `display` function.
5. End
Source Code :
#include <iostream>
#include <string>
class Book
private:
string name;
double price;
public:
// Default Constructor
Book()
price = 0.0;
// Parameterized Constructor
{
name = bookName;
price = bookPrice;
void display()
};
int main()
Book defaultBook;
defaultBook.display();
parameterizedBook.display();
copyBook.display();
Output :
ASSIGNMENT 4
AIM: WAP in C++ to create a class called as Distance, members are ft & in.
Assign appropriate values to objects D1 & D2 and add their values by
overloading binary ‘+’ operator to store the result in D3
Theoretical Explanation:
`Distance` Class: The `Distance` class includes two private data members, `feet`
and `inches`, which represent the feet and inches components of a distance.
Member Functions:
- `readDistance()`: This function is used to input the values for the feet and
inches components of a `Distance` object. It prompts the user to enter the feet
and inches and stores these values in the corresponding data members.
`main` Function:
- The `main` function begins by creating instances of the `Distance` class: D1,
D2, and D3.
- The user is prompted to enter the values for D1 and D2 using the
`readDistance()` function. The values are stored in these objects.
- The overloaded `+` operator is used to add D1 and D2, and the resulting
distance is stored in D3.
- The total distance, stored in D3, is displayed to the user using the
`dispDistance()` function.
Conclusion :
Algorithm:
1. Start
- Create three instances of the `Distance` class: D1, D2, and D3.
- Display a message prompting the user to enter the first distance, and call
the `readDistance()` function to input values for D1.
- Display a message prompting the user to enter the second distance, and call
the `readDistance()` function to input values for D2.
- Perform the addition of D1 and D2 using the overloaded `+` operator and
store the result in D3.
5. End
Source Code :
#include<iostream>
class Distance {
private:
public:
void readDistance() {
void dispDistance() {
cout << "Feet : " << feet << "\t" << "Inches : " << inches << endl;
Distance tempD;
tempD.inches = inches + dist1.inches;
return tempD;
};
int main() {
D1.readDistance();
D2.readDistance();
D3.dispDistance();
Output :
ASSIGNMENT 5
AIM: WAP in C++ to create a class called as student, having member as roll_no
& name of the student. Create another class Exam having members as mark1 &
mark2. Finally create a class called result which is derived from student &
Exam. Write a show function in it to show student info & percentage of marks
scored using Multiple Inheritance.
Theoretical Explanation:
`Student` Class: This base class contains data members for the roll number and
name of the student. It also provides a function to display this information.
- The parameterized constructor initializes the roll number and name when
an object is created.
`Exam` Class: This second base class includes data members for marks obtained
in two exams. It also provides a function to calculate the percentage of marks.
- The parameterized constructor initializes the marks for the two exams.
`Result` Class: This derived class inherits from both the `Student` and `Exam`
classes. It combines their functionality to show student information and
calculate the percentage of marks.
- The program starts by prompting the user to input the student's roll
number, name, and marks for the two exams.
- An instance of the `Result` class is created, and the input values are passed
to its constructor.
- The `Show()` function of the `Result` class is called to display the student's
information (roll number and name), marks for the two exams, and the
calculated percentage.
Conclusion :
Source Code :
#include<iostream>
#include<string>
class Student
protected:
int roll_no;
string name;
public:
void ShowStudentInfo()
}
};
class Exam
protected:
float mark1,mark2;
public:
float CalculatePercentage()
};
{
public:
void Show()
ShowStudentInfo();
};
int main()
int rollNo;
string studentName;
float marks1,marks2;
cout << "Enter Roll NO : ";
cin.ignore();
getline(cin,studentName);
Result result(rollNo,studentName,marks1,marks2);
result.Show();
return 0;
Output :
ASSIGNMENT 6
AIM: WAP in C++ to demonstrate Derived class constructor.
Theoretical Explanation:
This C++ program demonstrates the use of derived class constructors in object-
oriented programming. Here's how the program works:
`Base` Class: This is the base class, which contains private data members `a`,
`b`, and `c`. It also has a constructor. The constructor initializes values for `a`
and `b,` and then calls the `swap` function to swap these values. The `swap`
function displays the values of `a` and `b both before and after swapping.
`Derived` Class: This class is derived from the `Base` class. It has a constructor
of its own. The constructor displays a message indicating that the `Derived`
class constructor is called and then calls the `add` function. The `add` function
takes two integer arguments `x` and `y` (defaulting to 5 and 7 if not provided)
and displays the result of adding these values.
`main` Function: In the `main` function, an object of the `Derived` class named
`DerivedObject` is created. A message indicating the program's start is
displayed. This object's constructor automatically calls the constructors of both
the `Base` and `Derived` classes in order.
Conclusion :
In conclusion, this program illustrates the concept of derived class constructors
and the sequence in which they are called when an object of the derived class
is created. It demonstrates how base class constructors are invoked before
derived class constructors, and how member functions can be called from
within constructors to perform specific tasks during object initialization.
Algorithm:
1. Start
- Call the `swap` function to swap the values of `a` and `b`.
- In the `swap` function, display the values of `a` and `b before and after
swapping.
- Call the `add` function with default values for `x` and `y` (5 and 7,
respectively).
- In the `add` function, display the result of adding `x` and `y`.
- The constructors for the `Base` and `Derived` classes are automatically
called, in order.
- The `add` function is called from the `Derived` class's constructor, adding
the values 5 and 7.
5. End
Source Code :
#include<iostream>
class Base {
private:
int a, b, c;
public:
Base() {
a = 3;
b = 6;
swap();
void swap() {
cout << "Before Swap \n" << " A = " << a << "\n" << " B = " << b << endl;
c = a;
a = b;
b = c;
cout << "After Swap \n" << " A = " << a << "\n" << " B = " << b << endl;
}
};
public:
Derived() {
add();
cout << "Adding " << x << " and " << y << ": " << x + y << endl;
};
int main() {
Derived DerivedObject;
return 0;
Output :
ASSIGNMENT 7
AIM: WAP in C++ to implement friend function acting as a bridge between the
two classes.
Theoretical Explanation:
Class `A`: This class has a private data member, `privateA`, and a constructor
that initializes this private member. It declares the `bridgeFunction` as a friend,
allowing it to access the private members of `A`.
Class `B`: Similar to class `A`, it has a private data member, `privateB`, and a
constructor to initialize it. It also declares the `bridgeFunction` as a friend
`main` Function: In the `main` function, objects `objA` and `objB` of classes `A`
and `B` are created and initialized with values. Then, the `bridgeFunction` is
called with these objects as arguments. The function accesses and prints the
private members of both classes, effectively demonstrating how the friend
function bridges the gap between the two classes, allowing them to access
each other's private members.
Conclusion :
Algorithm:
1. Start
- It takes two objects, one of class `A` and one of class `B`, as parameters.
- In this function, it accesses the private members `privateA` from class `A`
and `privateB` from class `B` and prints their values.
- Create objects of class `A` and class `B`, named `objA` and `objB`, and
initialize them with values.
6. End
Source Code :
#include <iostream>;
class B;
class A {
private:
int privateA;
public:
A(int a) : privateA(a) {}
};
class B {
private:
int privateB;
public:
B(int b) : privateB(b) {}
};
void bridgeFunction(A objA, B objB) {
cout << "Accessing privateA from class A: " << objA.privateA << endl;
cout << "Accessing privateB from class B: " << objB.privateB << endl;
int main() {
A objA(10);
B objB(20);
bridgeFunction(objA, objB);
return 0;
Output :
ASSIGNMENT 8
AIM: WAP in C++ to implement Virtual function for function overriding.
Theoretical Explanation:
This C++ program demonstrates the concept of virtual functions for function
overriding, enabling polymorphism. Here's how the program works:
Base Class `base`: This class contains a public virtual function `print()`, which is
designed to be overridden by derived classes. It also has a public non-virtual
function `show()`.
Derived Class `derived`: This class inherits publicly from the `base` class and
overrides the virtual function `print()`. It also defines a non-virtual function
`show()`.
`main` Function: In the `main` function, the following steps are performed:
- The address of the derived class object `d` is assigned to the base class
pointer `bptr`.
- The `print()` function is called using the base class pointer `bptr`. Since it's
declared as virtual in the base class and overridden in the derived class, the
overridden version in the derived class is executed at runtime. This is an
example of dynamic polymorphism or late binding.
- The `show()` function is called using the base class pointer `bptr`. However,
since `show()` is not declared as virtual in the base class, the function call is
resolved at compile-time, and the base class's version of `show()` is executed.
This is an example of static polymorphism or early binding.
Conclusion :
In conclusion, this program illustrates the use of virtual functions for function
overriding, allowing a derived class to provide its own implementation of a
function declared in a base class. It demonstrates how virtual functions enable
polymorphism, enabling the selection of the appropriate function to call at
runtime.
Algorithm:
1. Start
- Declare a public virtual function `print()` in the base class. This function can
be overridden by derived classes.
- Inherit publicly from the base class, indicating that `derived` is a derived
class.
- Override the virtual function `print()` in the derived class. The overridden
function in the derived class has the same name and parameters as the base
class.
- Assign the address of the derived class object `d` to the base class pointer
`bptr`.
- Call the `print()` function using the base class pointer `bptr`. Since the
`print()` function is declared as virtual in the base class and overridden in the
derived class, the function call is resolved at runtime, and the derived class's
version of `print()` is executed.
- Call the `show()` function using the base class pointer `bptr`. Since the
`show()` function is not declared as virtual in the base class, the function call is
resolved at compile-time, and the base class's version of `show()` is executed.
5. End.
Source Code :
#include<iostream>
class base
public:
void show()
};
public:
void print()
void show()
};
int main()
base *bptr;
derived d;
bptr= &d;
return 0;
Output :
ASSIGNMENT 9
AIM:WAP in C++ to overload insertion (>>) & extraction (<<) operators for
objects.
Theoretical Explanation:
This C++ program demonstrates operator overloading for insertion (`>>`) and
extraction (`<<`) operators for objects of the `Complex` class. Here's how the
program works:
Complex Class: The `Complex` class represents complex numbers with real and
imaginary parts. It contains private data members `real` and `img`, a
parameterized constructor to set these values, and the overloaded `>>` and
`<<` operators.
Conclusion :
Algorithm:
1. Start
- Define private data members, `real` for the real part and `img` for the
imaginary part, both initialized to 0 by default.
- Define a function that takes an `istream` object `in` and a `Complex` object
`c` as arguments.
- Prompt the user to enter the real and imaginary parts of the complex
number.
- Read the values from the input stream and set them in the `Complex` object
`c`.
- Prompt the user to enter a complex number and use the overloaded `>>`
operator to read the input.
- Use the overloaded `<<` operator to display the complex object with a more
intuitive format.
6. End
Source Code :
#include <iostream>
class Complex {
private:
public:
Complex(int r = 0, int i = 0) {
real = r;
img = i;
};
in >> c.real;
in >> c.img;
return in;
}
if (c.img >= 0) {
} else {
return out;
int main() {
Complex c1;
cout << "The complex Object is: " << c1 << endl;
return 0;
Output :
ASSIGNMENT 10
AIM: WAP to implement function Template.
Theoretical Explanation:
This C++ program showcases the use of function templates, which are a
powerful feature of C++ that allows you to write generic functions that work
with different data types.
Conclusion :
1. Start
- Inside the function, a ternary conditional operator (`? :`) is used to compare
`x` and `y`. If `x` is greater than `y`, it returns `x`; otherwise, it returns `y`.
- The result is of type `T`, making this function template capable of working
with various data types.
- Use the `myMax` function template to find the maximum of two values of
different data types:
4. End
Source Code :
#include<iostream>
int main()
return 0;
Output :