0% found this document useful (0 votes)
2 views

CPP RECORD

The document outlines a series of C++ programming experiments, covering topics such as classes, constructors, operator overloading, inheritance, virtual functions, and exception handling. Each experiment includes an aim, procedure, source code, and results indicating successful execution. The document serves as a comprehensive guide for learning and demonstrating key C++ programming concepts.

Uploaded by

merita.281984
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)
2 views

CPP RECORD

The document outlines a series of C++ programming experiments, covering topics such as classes, constructors, operator overloading, inheritance, virtual functions, and exception handling. Each experiment includes an aim, procedure, source code, and results indicating successful execution. The document serves as a comprehensive guide for learning and demonstrating key C++ programming concepts.

Uploaded by

merita.281984
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/ 68

INDEX

S.NO Date Title of the Experiment Done Pg No. Signature

1. Class and Object

2. Constructor and Destructor

3. Friend Function

4. Function Overloading

5. Unary Operator Overloading

6. Binary Operator Overloading

7. Single Inheritance

8. Multilevel Inheritance

9. Multiple Inheritance

10. Hybrid Inheritance

11. Hierarchical Inheritance

12. Virtual Function

13. Exception Handling

14. Class Template

Function Template
15.

Command Line Argument


16.

1
Ex. No.: 01
CLASS AND OBJECT
Date :

Aim:
To write a C++ Program for Class and Object.

Procedure:

Step 1: Start the program.


Step 2: Define a Class Name.
Step 3: Declare variables inside the class.
Step 4: Define methods (functions) inside the class.
Step 5: Create objects of the class.
Step 6: Access Variable and methods using objects.
Step 7: Stop the Program.

2
Source Code:

#include <iostream>
using namespace std;
class Car
{
public:
string brand;
int year;
void displayInfo()
{
cout << "Car Brand: " << brand << endl;
cout << "Manufacturing Year: " << year << endl;
}
};
int main()
{
Car car1;
car1.brand = "Toyota";
car1.year = 2020;
car1.displayInfo();
return 0;
}

3
Sample Output:

4
Result:

The above program was verified and executed successfully.

5
Ex. No.: 02
Constructor and Destructor
Date :

Aim:
Write a Program to for Constructor and Destructor.

Procedure:

Step 1: Start the Process.


Step 2: Define a class.
Step 3: Declare variables inside the class.
Step 4: Define a constructor and It has the same name as the class.
Step 5: Define a destructor and It has the same name as the class but is preceded by a tilde (~).
Step 6: Create objects of the class.
Step 7: Stop the Process.

6
Source Code:

#include <iostream>
using namespace std;
class Car
{
public:
string brand;
int year;
Car(string b, int y)
{
brand = b;
year = y;
cout << "Constructor called for " << brand << endl;
}
~Car() {
cout << "Destructor called for " << brand << endl;
}
void displayInfo()
{
cout << "Car Brand: " << brand << endl;
cout << "Manufacturing Year: " << year << endl;
}
};

int main()
{
Car car1("Toyota", 2020);
car1.displayInfo();
return 0;
}
7
Sample Output

8
Result:

The above program was verified and executed successfully.

9
Ex. No.: 03
Friend Function
Date :

Aim:
To write a C++ Program to Demonstrate a Friend Function.

Procedure:

Step 1: Start the program.


Step 2: Define a class with private member.
Step 3: Declare a friend function inside the class using the friend keyword.
Step 4: Define the friend function outside the class.
Step 5: Create objects of the class.
Step 6: Call the friend function to access private members.
Step 7: Stop the program.

10
Source Code:

#include <iostream>
using namespace std;
class Box {
private:
int length;
public:
Box(int l) : length(l) {}

friend int calculateVolume(Box b);


};
int calculateVolume(Box b)
{
return b.length * b.length * b.length;
}
int main() {
Box b(5);
cout << "The volume of the cube is: " << calculateVolume(b) << endl;
return 0;
}

11
Sample Output:

12
Result:

The above program was verified and executed successfully.

13
Ex. No.: 04 Function Overloading

Date :

Aim:
To write a C++ program to demonstrate Function Overloading.

Procedure:

Step 1: Start the program.


Step 2: Define a class (optional) or use standalone functions.
Step 3: Declare multiple functions with the same name but different parameters.
Step 4: Define the functions with their respective logic.
Step 5: Call the functions with different argument types.
Step 6: Stop the Program.

14
Source Code:

#include <iostream>
using namespace std;
int plusFuncInt(int x, int y) {
return x + y;
}
double plusFuncDouble(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}

15
Sample Output:

16
Result:

The above program was verified and executed successfully.


17
Ex. No.: 05 Unary Operator Overloading

Date :

Aim:

To write a C++ Program for Unary Operator Overloading.

Procedure:

Step 1: Start the program.


Step 2: Define a class with attributes.
Step 3: Declare a member function to overload the unary operator.
Step 4: Use the operator keyword followed by the operator symbol.
Step 5: Define the overloaded operator function.
Step 6: Create objects and apply the overloaded operator.
Step 7: Stop the Program.

18
Source code:

#include <iostream>
using namespace std;
class Complex {
int a, b;
public:
Complex() : a(0), b(0)
{
}
void getvalue() {
cout << "Enter the Two Numbers: ";
cin >> a >> b;
}
void operator++(int)
{
a++;
b++;
}
void operator--(int)
{
a--;
b--;
}
void display()
{
cout << a << " + " << b << "i" << endl;
}
};
int main()
{
Complex obj;
obj.getvalue();

19
obj++;
cout << "Incremented Complex Number:\n";
obj.display();
obj--;
cout << "Decremented Complex Number:\n";
obj.display();
return 0;
}

20
Sample Output :

21
Result:

The above program was verified and executed successfully.

22
Ex. No.: 06
Binary Operator Overloading
Date :

Aim:
To write a C++ program for Binary Operator Overloading

Procedure:

Step 1: Start the program.


Step 2: Define a class with attributes.
Step 3: Declare a member function to overload the binary operator.
Step 4: Use the operator keyword followed by the operator symbol.
Step 5: Create objects and apply the overloaded operator.
Step 6: Stop the program.

23
Source Code:

#include <iostream>
using namespace std;
class complex
{
int a, b;
public:
void get_data(){
cout << "Enter the value of Complex Numbers a, b: "; cin >> a >> b;
}
complex operator+(complex ob)
{
complex t;
t.a = a + ob.a;
t.b = b + ob.b;
return (t);
}
complex operator-(complex ob)
{
complex t;
t.a = a - ob.a;
t.b = b - ob.b;
return (t);
}
void display(){
cout << a << "+" << b << "i" << "\n";
}
};
int main()
{
complex obj1, obj2, result, result1;
24
obj1.get_data();
obj2.get_data();
result = obj1 + obj2;
result1 = obj1 - obj2;
cout << "\n\nInput Values:\n";
obj1.display();
obj2.display();
cout << "\nResult:";
result.display();
result1.display();
return 0;
}

25
Sample Output:

26
Result:

The above program was verified and executed successfully.

27
Ex. No.: 07 Single Inheritance
Date :

Aim:
Write a C++ program to demonstrate Single Inheritance.

Procedure:

Step 1: Start the program.


Step 2: Define a base class with attributes and methods.
Step 3: Define a derived class that inherits from the base class using the (:) symbol.
Step 4: Inside the derived class, access base class members.
Step 5: Create objects of the derived class.
Step 6: Stop the Program.

28
Source Code:

#include <iostream>
#include <conio.h>
using namespace std;
class inheritance
{
public:
inheritance()
{
cout<<"/n base class constructor";
}
};
class derived:public inheritance
{
public:
derived()
{
cout<<"/n derived class constructor";
}
};
int main()
{
derived d;
return(0);
}

29
Sample Output:

30
Result:

The above program was verified and executed successfully.

31
Ex. No.: 08
Multilevel inheritance
Date :

Aim:

To write a C++ Program to demonstrate Multilevel Inheritance.

Procedure:

Step 1: Start the program.


Step 2: Define a base class with attributes and methods.
Step 3: Define an intermediate derived class that inherits from the base class.
Step 4: Define a final derived class that inherits from the intermediate class.
Step 5: Create an object of the final derived class.
Step 6: Stop the Program.

32
Source Code:

#include <iostream>
using namespace std;

class MyClass {
public:
void myFunction() {
cout << "Inherits the parent class." ;
}
};
class MyChild: public MyClass {
};
class MyGrandChild: public MyChild {
};
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}

33
Sample Output:

34
Result:

The above program was verified and executed successfully.

35
Ex. No.: 09
Multiple inheritance
Date :

Aim:

To write a C++ Program to demonstrate Multiple Inheritance.

Procedure:
Step 1: Start the program.
Step 2: Define two or more base classes with attributes and methods.
Step 3: Define a derived class that inherits from multiple base classes.
Step 4: Inside the derived class, use functions from both base classes.
Step 5: Create an object of the derived class.
Step 6: Stop the Program.

36
Source Code:
#include <iostream>
using namespace std;

class A {
public:
void display() {
cout<<"Inherit the class B and Class A"<<endl;

}
};

class B : public A {};

class C : public B {};

int main() {
C obj;
obj.display();
return 0;
}

37
Sample Ouput:

38
Result:

The above program was verified and executed successfully.

39
Ex. No.: 10
Hybrid Inheritance
Date :

Aim:
To write a C++ program to demonstrate Hybrid Inheritance.

Procedure:

Step 1: Start the program.


Step 2: Define the base class with common attributes and methods.
Step 3: Define two intermediate classes that inherit from the base class using single inheritance.
Step 4: Define a derived class that inherits from multiple intermediate classes.
Step 5: Create an object of the final derived class.
Step 6: Stop the Program.

40
Source Code:

#include <iostream>
using namespace std;
class first
{
public:
double length,breath;

};
class second:public first
{
public:
double area (double l,double b)
{
length=l;
breath=b;
return l*b;
}
};
class third : protected first
{
public:
double perimeter(double l,double b)
{
length=l;
breath=b;
return 2*(l+b);
}
};
int main()
{
41
second s;
third t;
cout<<"Area of rectangle :"<<s.area(2,3)<<endl;
cout<<"Perimeter of rectangle :"<<t.perimeter(4,5)<<endl;
return 0;
}

42
Sample Output:

43
Result:

The above program was verified and executed successfully.

44
Ex. No.: 11
Hierarchical Inheritance
Date :

Aim:
To write a C++ program to demonstrate Hierarchical Inheritance.

Procedure:

Step 1: Start the program.


Step 2: Define the Base Class.
Step 3: Define Multiple Derived Classes.
Step 4: Each derived class can override or extend the behavior of the base class if needed.
Step 5: Instantiate Objects of Derived Classes.
Step 6: Access Properties and Methods.
Step 7: Stop the program.

45
Source Code;
#include <iostream>
using namespace std;
class Animal {
public:
void info() {
cout << "This is an animal." << endl;
}};
class Dog : public Animal {
public:
void bark() {
cout << "It is a Dog. Woof woof." << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "It is a Cat. Meow." << endl;
}
};
int main() {
Dog dog1;
cout << "Dog Class:" << endl;
dog1.info();
dog1.bark();
Cat cat1;
cout << "\nCat Class:" << endl;
cat1.info();
cat1.meow();
return 0;
}

46
Sample Output:

47
Result:

The above program was verified and executed successfully.

48
Ex. No.: 12
Virtual function
Date :

Aim:

To write a C++ program to demonstrate Virtual Function.

Procedure:

Step 1: Start the program.


Step 2: Define a base class with a function marked as virtual keyword.
Step 3: Define a derived class that inherits from the base class.
Step 4: Declare a Base Class Pointer or Reference.
Step 5: Assign Derived Class Object to Base Class Pointer/Reference
Step 6: Call the Virtual Function Using Base Class Pointer/Reference
Step 7: Stop the program

49
Source Code:

#include <iostream>
using namespace std;

class parent
{
public:
virtual void show()
{
cout << "Base class\n";
}
};

class child:public parent


{
public: void show()
{
cout << "Derived Class";
}
};

int main()
{
parent* p;
child c;
p = &c;
c.show();
}

50
Sample Output:

51
Result:

The above program was verified and executed successfully.

52
Ex. No.: 13 Exception Handling
Date :

Aim:

To write a C++ program for Exception Handling.

Procedure:

Step 1: Start the program.


Step 2: Identify Potential Error Sources.
Step 3: Use the try Block defines a region of code that will be checked for exceptions.
Step 4: Inside the try block, when an error occurs, use the throw keyword to throw an
exception.
Step 5: After the try block, define one or more catch blocks that will handle specific types of
exceptions.
Step 6: Stop the program.

53
Source Code:

#include <iostream>
using namespace std;
int main() {
try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw (age);
}
}
catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Age is: " << myNum;
}
return 0;
}

54
Sample Output:

55
Result:

The above program was verified and executed successfully.

56
Ex. No.: 14 Class Template
Date :

Aim:

To write a C++ program for Class Template.

Procedure:

Step 1: Start the program.

Step 2: Define the Template with the template Keyword.

Step 3: Create a class with member functions and member variables.

Step 4: Instantiate the Class Template.

Step 5: Once the class template is instantiated, you can call its functions and access its
members.

Step 6: Stop the Program.

57
Source Code:

#include <iostream>
using namespace std;
template <class T>
class Number {
private:
T num;
public:
Number(T n) : num(n) {}
T getNum() {
return num;
}
};
int main() {
Number<int> numberInt(7);
Number<double> numberDouble(7.7);
cout << "int Number = " << numberInt.getNum() << endl;
cout << "double Number = " << numberDouble.getNum() << endl;

return 0;
}

58
Sample Output:

59
Result:

The above program was verified and executed successfully.


60
Ex. No.: 15
Function Template
Date :

Aim:

To write a C++ program for Function Template.

Procedure:

Step 1: Start the program.


Step 2: Declare the function template using the template keyword followed by a template
parameter.
Step 3: Implement the Function Template.
Step 4: Instantiate the Function Template.
Step 5: Call the function template with different data types.
Step 6: Stop the program.

61
Source Code:

#include <iostream>
using namespace std;
template <typename T>
T add(T num1, T num2) {
return (num1 + num2);
}
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;
return 0;
}

62
Sample Output:

63
Result:

The above program was verified and executed successfully.

64
Ex. No.: 16
Command Line Argument
Date :

Aim:

To write a C++ program for Command Line Argument.

Procedure:

Step 1: Start the program.


Step 2: Declare three variables (n1, n2, n3) of type double to store the three numbers entered
by the user.
Step 3: Use cin to read the three numbers entered by the user and store them in n1, n2, and n3.
Step 4: Use if-else statements to compare the three numbers.
Step 5: Return 0 from the main function to indicate successful execution.
Step 6: Stop the program.

65
Source Code:

#include <iostream>
using namespace std;
int main()
{
double n1, n2, n3;
cout << "Enter three numbers: "<<endl;
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
return 0;
}

66
Sample Output:

67
Result:

The above program was verified and executed successfully.

68

You might also like