0% found this document useful (0 votes)
44 views15 pages

Experiment

Uploaded by

sachetanarora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views15 pages

Experiment

Uploaded by

sachetanarora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Experiment-18

Define Class named point which represents 2-D Point, i.e P(x, y). Define
Default constructor to initialize both data member value 5, Parameterized
constructor to initialize member according to the value supplied by the
user and Copy Constructor. Define Necessary Functions and Write a
program to test class Point.

Theory :

The Point class represents a 2D point with x and y coordinates. It includes


a default constructor that initializes both x and y to 5, a parameterized
constructor that allows user-defined values for x and y, and a copy
constructor that creates a new Point object by copying the coordinates of
an existing one. The class also provides getter and setter methods for
accessing and modifying the coordinates, as well as a function to display
the point’s values. The program demonstrates the use of these
constructors and methods by creating and manipulating several Point
objects.

Code:

#include <iostream>

using namespace std;

class Point {

int x, y;

public:

Point() : x(5), y(5) {}

Point(int xVal, int yVal) : x(xVal), y(yVal) {}

Point(const Point& p) : x(p.x), y(p.y) {}

int getX() const { return x; }

int getY() const { return y; }

void setX(int xVal) { x = xVal; }


void setY(int yVal) { y = yVal; }

void display() const { cout << "Point(" << x << ", " << y << ")" <<
endl; }

};

int main() {

Point p1;

p1.display();

Point p2(10, 20);

p2.display();

Point p3(p2);

p3.display();

p3.setX(15);

p3.setY(25);

p3.display();

return 0;

}
Output:

Conclusion:

The program has been compiled and run successfully and the output has
been verified.
Experiment-19

Create a C++ class named `Employee` to model an employee in a


company. The class should use constructors to initialize its objects with
specific attributes: `employeeID` (integer), `name` (string), `position`
(string), and `salary` (double). Define the following methods:

Theory ;

The Employee class is designed to encapsulate the attributes and


behavior of an employee in a company. It uses a parameterized
constructor to initialize the employee's employeeID, name, position, and
salary. The class includes getter and setter methods to access and modify
these private data members, ensuring encapsulation and data integrity.
Additionally, a display() method is provided to output the employee’s
details, and an updateSalary() method allows for dynamic adjustment of
the salary based on a percentage increase. This structure models real-
world scenarios where employee data needs to be managed and updated
efficiently.

Code:

#include <iostream>

#include <string>

using namespace std;

class Employee {

int employeeID;

string name, position;

double salary;

public:

Employee(int id, string empName, string empPosition, double


empSalary)
: employeeID(id), name(empName), position(empPosition),
salary(empSalary) {}

int getEmployeeID() const { return employeeID; }

string getName() const { return name; }

string getPosition() const { return position; }

double getSalary() const { return salary; }

void setName(string empName) { name = empName; }

void setPosition(string empPosition) { position = empPosition; }

void setSalary(double empSalary) { salary = empSalary; }

void display() const {

cout << "Employee ID: " << employeeID

Output:

Conclusion:

The program has been compiled and run successfully and the output has
been verified.
Experiment 20

Define a class `Complex` with `real` and `imaginary` as two data


members, along with default and parameterized constructors. The class
should include functions to initialize and display the data. Additionally,
overload the `+` operator to add two complex objects. Write a complete
C++ program to demonstrate the use of the `Complex` class.

Theory:

The Complex class models a complex number with two data members:
real and imaginary, representing the real and imaginary parts,
respectively. It includes a default constructor that initializes both parts to
0, and a parameterized constructor that allows specific initialization of
these values. The class also provides a display() method to output the
complex number in the form a + bi. Additionally, the + operator is
overloaded to enable the addition of two Complex objects by summing
their real and imaginary parts, returning a new Complex object.

Code:

#include <iostream>

using namespace std;

class Complex {

private:

double real, imaginary;

public:

Complex() : real(0), imaginary(0) {}

Complex(double r, double i) : real(r), imaginary(i) {}

Complex operator + (const Complex& other) {


return Complex(real + other.real, imaginary + other.imaginary);

void display() const {

cout << real << " + " << imaginary << "i" << endl;

};

int main() {

Complex c1(3.5, 2.5);

Complex c2(1.5, 3.5);

c1.display();

c2.display();

Complex c3 = c1 + c2;

c3.display();

return 0;

Output:

Conclusion;
The program has been compiled and run successfully and the output has
been verified.

Experiment 21

Design three classes: Student, Exam, and Result. The Student class has
data members such as roll number and name, etc. Create a class Exam by
inheriting the Student class. The Exam class adds data members
representing the marks scored in six subjects. Derive the Result class from
the Exam class, which has its own members such as total marks. Write an
interactive program to model this relationship. What type of inheritance
does this model belong to?

Theory:

In this experiment, we design three classes: Student, Exam, and Result,


utilizing inheritance to model a hierarchical relationship. The Student class
contains data members such as roll number and name to represent basic
student information. The Exam class inherits from the Student class,
adding data members for marks scored in six subjects, effectively
extending the functionality of the Student class. The Result class is
derived from the Exam class and introduces its own member for total
marks, which calculates the aggregate score from the marks obtained in
the subjects.

Code:

#include <iostream>

#include <string>

using namespace std;

class Student {

protected:

int rollNumber;

string name;
public:

void input() {

cout << "Enter Roll Number and Name: ";

cin >> rollNumber;

cin.ignore();

getline(cin, name);

void display() const {

cout << "Roll Number: " << rollNumber << "\nName: " << name <<
endl;

};

class Exam : public Student {

protected:

double marks[6];

public:

void inputMarks() {

cout << "Enter marks for 6 subjects:\n";

for (double &mark : marks) cin >> mark;

void displayMarks() const {

for (int i = 0; i < 6; ++i)

cout << "Subject " << (i + 1) << ": " << marks[i] << endl;

};
class Result : public Exam {

public:

void displayResult() const {

double totalMarks = 0;

for (double mark : marks) totalMarks += mark;

display();

displayMarks();

cout << "Total Marks: " << totalMarks << endl;

};

int main() {

Result studentResult;

studentResult.input();

studentResult.inputMarks();

studentResult.displayResult();

return 0;

Output:
Conclusion-The program has been compiled and run successfully and the
output has been verified.

Experiment 22

Write a program to swap two numbers (create two classes) using a friend
function.

Theory:

In this program, we create two classes to demonstrate the swapping of


two numbers using a friend function. The first class, Swap, contains two
private data members representing the numbers to be swapped. The
second class, Swapper, includes a friend function that has access to the
private members of the Swap class. This function takes an instance of the
Swap class as a parameter and swaps its two numbers. By leveraging the
friend function, we encapsulate the swapping logic while allowing access
to private data, showcasing the concept of friendship in C++.

Code:

#include <iostream>

using namespace std;

class Swap; // Forward declaration

class Swapper {

public:

void swapNumbers(Swap& s);

};

class Swap {

private:

int num1, num2;

public:
void input() {

cout << "Enter two numbers: ";

cin >> num1 >> num2;

void display() const {

cout << "Numbers after swapping: " << num1 << " and " << num2
<< endl;

friend void Swapper::swapNumbers(Swap& s);

};

void Swapper::swapNumbers(Swap& s) {

int temp = s.num1;

s.num1 = s.num2;

Output:
Conclusion-

The program has been compiled and run successfully and the output has
been verified.

Experiment-23

Write a program to calculate the total marks of a student using the


concept of a virtual base class.

Theory:

This program demonstrates the use of virtual base classes to avoid


ambiguity in the case of multiple inheritance. The class Result inherits
from both Test and Sports, which virtually inherit from Student. This avoids
duplication of the rollNo attribute.

Code:

#include <iostream>

using namespace std;

// Virtual base class

class Marks {

public:

virtual void getMarks() = 0; // Pure virtual function

};

// Derived class for Subject 1

class Subject1 : virtual public Marks {

protected:

float marks1;

public:

void getMarks() override {


cout << "Enter marks for Subject 1: ";

cin >> marks1;

};

// Derived class for Subject 2

class Subject2 : virtual public Marks {

protected:

float marks2;

public:

void getMarks() override {

cout << "Enter marks for Subject 2: ";

cin >> marks2;

};

// Class to calculate total marks

class TotalMarks : public Subject1, public Subject2 {

public:

void getMarks() override {

Subject1::
Output:

Conclusion-

The program has been compiled and run successfully and the output has
been verified.

You might also like