0% found this document useful (0 votes)
8 views2 pages

C++ Class Design and Operator Overloading

The document outlines six programming questions that require the creation of various classes in C++. Each question specifies the attributes and methods for classes such as Rational, Distance, BankAccount, Student, InventoryItem, and Vehicle, focusing on constructors, operator overloading, and member functions. The main function in each question is to demonstrate the functionality of the respective class and its methods.

Uploaded by

annaswinning8989
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)
8 views2 pages

C++ Class Design and Operator Overloading

The document outlines six programming questions that require the creation of various classes in C++. Each question specifies the attributes and methods for classes such as Rational, Distance, BankAccount, Student, InventoryItem, and Vehicle, focusing on constructors, operator overloading, and member functions. The main function in each question is to demonstrate the functionality of the respective class and its methods.

Uploaded by

annaswinning8989
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

Question-1: Write a program to test your class called Rational for performing arithmetic operations.

Use
integer variables to represent the class's private data the num (as numerator) and the dnum (as denominator).
Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor
should contain default values in case no initializers are provided and should store them. Provide public member
functions that perform each of the following tasks:
i. get two Rational numbers by getrational().
ii. Add two multiplying rational numbers using the operator +, *.
iii. Compare two Rational numbers by using the operator ==.
iv. Display Rational numbers in the form a/b, where a is the numerator and b is the denominator.
Write the main function to create Rational objects and define the appropriate member functions as const
member functions of the above-said member functions.

Question-2: Declare a class Distance with two data members’ feets (int) and inches (float). The class includes
getdist() and showdist() members function for entering the data, and displaying the data members, it also
overloads the +, -, *, >, == operator to add, subtract, multiply two distance objects also check the two objects
are greater or equals. The class also has no and two argument constructors. Define appropriate members
function as the const member function. Write the main function to create distance objects and used the above-
said member functions.
Question-3: Define a class for a BankAccount that includes the Name, Account No, CNIC, account type and
balance amount in the account of the depositor with appropriate data type and access modifiers.
The class also contains the following member functions:
 A constructor to assign "initial values
 Deposit function to deposit some amount. It should accept a positive amount as a parameter.
 Withdraw function [Overload binary +] to withdraw an amount after checking. It should accept the
amount as a parameter.
 Display function to display name and balance after checking/verifying the CNIC No.
 Overload the binary + operator that adds the balance of one account to another account if both accounts
belong to one person.
Create a main () function that declares three objects of BankAccount. Makes the appropriate members function
as the const member function. Demonstrates that each function works correctly.

Question-4: Create a class Student with attributes: name, roll number, CNIC, marks in three subjects, and
grade.
 Constructor to initialize all attributes.
 calculateGrade() to compute grade based on average marks.
 Overload the > operator to compare two students by average marks.
 Overload the += operator to add bonus marks to all subjects.
 display(string cnic) to show student details after verifying CNIC.
Create three student objects and demonstrate grade calculation, comparison, bonus addition, and CNIC-
based display.
Question-5: Define a class InventoryItem that includes the item name, item code, quantity, and price per unit.
Use appropriate access modifiers and data types.
 Constructor to initialize all attributes.
 restock(int amount) to increase quantity.
 sell(int amount) to reduce quantity if enough stock is available.
 Overload the == operator to compare two items by item code.
 Overload the * operator to calculate total value (quantity * price).
 Display function to show item details.
Create a main () function that declares three objects of InventoryItem. Makes the appropriate members function
as the const member function. Demonstrates that each function works correctly.
Question-6: Define a class Vehicle with attributes: owner name, registration number, CNIC, vehicle type, and
engine capacity.
 Constructor to initialize all attributes.
 updateEngineCapacity(double newCapacity) to modify engine size.
 Overload the == operator to check if two vehicles belong to the same owner (by CNIC).
 verifyOwnership(string cnic) to confirm ownership.
Create the vehicle objects and demonstrate engine update, ownership verification, and operator overloading.

You might also like