C++ Programs using Class and Object with Examples| Programs of Classes and Objects in C++

1. Write a simple C++ program using class and object.

Solution:

Output:

Maruti is a car which has 4 wheels

It has 2 doors

Output:

Roll Number: 1

Name: Aakash Shah

2. Write a program to add two numbers using class and objects.

Solution:

OR

Output:

Enter 2 numbers: 45 56

Result: 101

3. Write a C++ program to add and subtract two numbers using class.

Solution:

Output:

Enter 2 numbers: 4 5

Sum of 4 and 5 is: 9

Enter 2 numbers: 6 3

Difference of 6 and 3 is: 3

4. Write a C++ program to multiply and divide two numbers using class.

Solution:

Output:

Enter 2 numbers: 4 5

Product of 4 and 5 is: 20

Enter 2 numbers: 6 3

Division of 6 and 3 is: 2

5. Define a class student for the following specifications:

Private members of the student are:

Registration number, Age, Name and Address

Public members of the student are:

input reads registration number, name, age and address

display prints the data

Solution:

Output:

Enter data of students…

1.Registration Number: 1001

2.Name: Ankit

3.Age: 20

4.Address: Tinsukia

Student data is:

1.Registration Number: 1001

2.Name: Ankit

3.Age: 20

4.Address: Tinsukia

6. Write a C++ program to find biggest number of three numbers using class and object.

Solution:

Output:

Largest Number:42

7. Write a C++ program to calculate area and volume of a room using class and object.

Solution:

Output:

Area of Room: 2211.3

Volume of Room: 56167

8. Define a class student with the following specification:

    Private members of class student

    rollno integer

    name  character

    eng, math, sc, tot   float

    calc_tot()      A function to calculate eng+math+sc with float return type

    Public member functions of class student

    Takedata() function to accept values for rollno, name, eng, math, sc and invoke calc_tot() to calculate total. Showdata() function to display all the data members on the screen.

    Leave a Comment