C++ Practice programs using Class and Objects

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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

Output:

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.

Output:

9. Define a class BOOK with the following specification:

Private members of the class BOOK are

book_no        integer type

book_title     20 character

price               float

total_cost()   A function to calculate total cost for n number of copies, where n is passed to the function as argument.

Public members of the class BOOK are

input()           Function to read book_no, book_title, price

purchase()    Function to ask the user to input the number of copies to be purchased. It invokes total_cost() and prints the total cost to be paid by the user.

Output:

10. A class has to be declared for a salesman with the characteristics like salesman number, salesman name, product number, target and sales made, and commission. The operations that can be performed on this data are: calculate commission and print commission.

Output:

11. Define a class Library for the following specifications:

Private members of the Library are

Name array of characters of size 20

eng_book, sc_book, others and tot_book integers

compute function that calculates the total number of books and return the total. tot_book is sum of eng_book, sc_book and others.

Public members of the Library are:

readData accepts the data and involves the compute function

printData prints the data.

Output:

12. Write a C++ program to simulate result preparation system for a student. The data are given below.

Define a class student:

Private members of the class Student

rollno integer

name  array of character

grade character

marks array of integer

tot,per           float

calc_tot()      A function to calculate total marks with float return type

Public member functions of class student

getval() function to accept values for rollno, name and marks to calculate total. Showdata() function display all the data.

The percentage marks are the average marks and the grade is calculated as follows:

Percentage marks      grade

<50                                         ‘F’

<60                                         ‘D’

<70                                         ‘C’

<80                                         ‘B’

Above 80                              ‘A’

Output:

13. Declare a class to represent a bank account.

Include the following members:

Name of the depositor, Account Number, Type of account (S for savings and C for current), Balance amount.

The class also contains member functions to do the following:

  • To initialize data members
  • To deposit money
  • To withdraw money after checking the balance.
  •  To display the data members.

Output:

14. Write a C++ program to store data of 5 employee using array of objects.

Output:

15. Write a C++ program to simulate result preparation system for 3 students. The data available for each student includes rollno, name and marks in 3 subjects. Calculate percentage and grade. The percentage marks are the average marks and the grade is calculated as follows:

Percentage marks               grade

<50                                         ‘F’

<60                                         ‘D’

<70                                         ‘C’

<80                                         ‘B’

Above 80                              ‘A’

Output:

Leave a Comment