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.

Solution:

Output:

Enter name: Akash Singh

Enter roll number: 1

Enter Marks –

————

English: 80

Mathematics: 90

Science: 95

Roll Number: 1

Name: Akash Singh

Marks –

—————-

English: 80

Math: 90

Science: 95

Total Marks: 265

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.

Solution:

Output:

Enter Book No: 101

Enter Book Title: Let Us C

Enter Price: 350

Enter the number of copies to be purchased: 4

Total Amount is: 1400

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.

Solution:

Output:

Enter Salesman No: 101

Enter Salesman Name: Akash Sah

Enter Product No: 123

Enter target and sales made: 200 400

——————————

Salesman No: 101

Salesman Name: Akash Sah

Commission: 80

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.

Solution:

Output:

Enter the name: College Library

Enter the number of English books: 100

Enter the number of Science books: 150

Enter the number of other books: 300

————————

Name: College Library

Number of English Books: 100

Number of Science Books: 150

Number of Other Books 300

Total Books: 550

12. Write a C++ program to simulate result preparation system for 5 students. The data available for each student includes rollno, name and marks in 3 subjects. Calculate percentage and grade.

    Define a class student:

    Private members of the class:

    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

    Solution:

    Output:

    Enter student data……

    Roll Number: 1

    Name: Anuran Kashyap

    Enter Marks:

    Subject 1: 90

    Subject 2: 85

    Subject 3: 96

    Roll Number: 2

    Name: Akash Singh

    Enter Marks:

    Subject 1: 78

    Subject 2: 80

    Subject 3: 94

    Roll Number: 3

    Name: Nibir Dutta

    Enter Marks:

    Subject 1: 67

    Subject 2: 78

    Subject 3: 80

    Roll Number: 4

    Name: Priya Sharma

    Enter Marks:

    Subject 1: 85

    Subject 2: 76

    Subject 3: 90

    Roll Number: 5

    Name: Aditya Moran

    Enter Marks:

    Subject 1: 80

    Subject 2: 76

    Subject 3: 67

    Student Data

    ——————

    Roll Number: 1

    Name: Anuran Kashyap

    Percentage: 90.33

    Grade: A

    Roll Number: 2

    Name: Akash Singh

    Percentage: 84.00

    Grade: A

    Roll Number: 3

    Name: Nibir Dutta

    Percentage: 75.00

    Grade: B

    Roll Number: 4

    Name: Priya Sharma

    Percentage: 83.67

    Grade: A

    Roll Number: 5

    Name: Aditya Moran

    Percentage: 74.33

    Grade: B

    13. Declares 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.

      Solution:

      Output – 1:

      Enter details:

      —————

      Account Number: 23415678909876

      Name: Eliza Begum

      Account type Saving/Current(S/C): S

      Balance: 100000

      Enter deposit amount: 50000

      Amount Deposited…..

      Current Balance is: 150000

      Enter withdraw amount: 10000

      —————–

      Account Number: 23415678909876

      Name: Eliza Begum

      Account Typr: S

      Balance (Rs.):140000

      Output -2:

      Enter details:

      —————

      Account Number: 12345678909876

      Name: Moidur Rahman

      Account type Saving/Current(S/C): D

      Enter S or C only

      Account type Saving/Current(S/C): S

      Balance: 50000

      Enter deposit amount: 10000

      Amount Deposited…..

      Current Balance is: 60000

      Enter withdraw amount: 100000

      Cannot withdraw! Not sufficient amount

      —————–

      Account Number: 12345678909876

      Name: Moidur Rahman

      Account Typr: S

      Balance (Rs.):60000

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

        Solution:

        Output:

        Leave a Comment