C++_Programs
C++_Programs
Aim:
To write a C++ program to implement class and object.
Algorithm:
1. Start the program.
2. Declare a class named 'add' with data members 'a', 'b', and 'sum'.
3. Create member functions 'getdata()' to input values and 'add_data()' to perform
addition.
4. In the main function, create an object of the class.
5. Call the 'getdata()' and 'add_data()' functions using the object.
6. Display the result of the addition.
7. Stop the program.
Program:
#include <iostream.h>
#include <conio.h>
class add {
int a, b, sum;
public:
void getdata() {
cout << "Enter the First value: ";
cin >> a;
cout << "Enter the Second value: ";
cin >> b;
}
void add_data() {
sum = a + b;
cout << "The Result = " << sum;
}
};
void main() {
clrscr();
cout << "\tImplementation Of Class and Object\n";
add al;
al.getdata();
al.add_data();
getch();
}
Output:
Result:
Thus, the C++ program for the implementation of Class and Object was successfully
executed and the output was verified.
2. Implementation of Constructor
Aim:
To write a C++ program to implement constructor.
Algorithm:
8. Start the program.
9. Declare a class named 'Rectangle' with data members 'length' and 'breadth'.
10. Create a constructor to initialize the values and calculate the area.
11. In the main function, create an object of the class.
12. The constructor will automatically execute and display the area.
13. Stop the program.
Program:
#include <iostream.h>
#include <conio.h>
class Rectangle {
int length, breadth;
public:
Rectangle() {
cout << "Enter your length: ";
cin >> length;
cout << "Enter your breadth: ";
cin >> breadth;
cout << "\nArea of rectangle = " << length * breadth;
}
};
void main() {
clrscr();
cout << "\tImplementation of Constructor\n";
Rectangle obj;
getch();
}
Output:
Implementation of Constructor
Result:
Thus, the C++ program for the implementation of Constructor was successfully executed
and the output was verified.
3. Single Inheritance
Aim:
To write a C++ program to implement Single Inheritance.
Algorithm:
14. Start the program.
15. Declare a base class with a display function.
16. Declare a derived class that inherits the base class.
17. Define display functions for both classes.
18. In the main function, create an object of the derived class.
19. Call both display functions using the derived class object.
20. Stop the program.
Program:
#include <iostream.h>
#include <conio.h>
class base {
public:
void display() {
cout << "BASE CLASS\n";
}
};
void main() {
clrscr();
cout << "\tSingle Inheritance\n";
derived d;
d.display();
d.display1();
getch();
}
Output:
Single Inheritance
BASE CLASS
DERIVED CLASS
Result:
Thus, the C++ program for the implementation of Single Inheritance was successfully
executed and the output was verified.