Experiment
Experiment
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 :
Code:
#include <iostream>
class Point {
int x, y;
public:
void display() const { cout << "Point(" << x << ", " << y << ")" <<
endl; }
};
int main() {
Point p1;
p1.display();
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
Theory ;
Code:
#include <iostream>
#include <string>
class Employee {
int employeeID;
double salary;
public:
Output:
Conclusion:
The program has been compiled and run successfully and the output has
been verified.
Experiment 20
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>
class Complex {
private:
public:
cout << real << " + " << imaginary << "i" << endl;
};
int main() {
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:
Code:
#include <iostream>
#include <string>
class Student {
protected:
int rollNumber;
string name;
public:
void input() {
cin.ignore();
getline(cin, name);
cout << "Roll Number: " << rollNumber << "\nName: " << name <<
endl;
};
protected:
double marks[6];
public:
void inputMarks() {
cout << "Subject " << (i + 1) << ": " << marks[i] << endl;
};
class Result : public Exam {
public:
double totalMarks = 0;
display();
displayMarks();
};
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:
Code:
#include <iostream>
class Swapper {
public:
};
class Swap {
private:
public:
void input() {
cout << "Numbers after swapping: " << num1 << " and " << num2
<< endl;
};
void Swapper::swapNumbers(Swap& s) {
s.num1 = s.num2;
Output:
Conclusion-
The program has been compiled and run successfully and the output has
been verified.
Experiment-23
Theory:
Code:
#include <iostream>
class Marks {
public:
};
protected:
float marks1;
public:
};
protected:
float marks2;
public:
};
public:
Subject1::
Output:
Conclusion-
The program has been compiled and run successfully and the output has
been verified.