Unit 1: OOP implementation in C++
Object Oriented Programming using C++ Laboratory notes · PTU syllabus (UGSEC2504)
On this page
Unit summary
This lab puts the C++ theory into practice: input/output with cin and cout, functions and reference variables, classes and arrays of objects, function overloading, inheritance hierarchies, operator overloading, virtual functions and file handling. Below are model solutions and the logic behind the key experiments.
After this unit you can
- Write C++ programs using cin/cout, functions and reference variables
- Build classes such as a bank account and use arrays of objects
- Implement inheritance hierarchies and operator overloading
- Use virtual functions, abstract classes and file streams
PTU syllabus topics
- Marks entry and totaling with cin/cout
- reference-variable swap function
- largest-of-three function
- factorial program
- bank account class with deposit/withdraw/display
- array-of-objects for multiple accounts
- function overloading for triangle area calculation
- Publication/Book/Tape inheritance hierarchy
- Student/Exam/Result inheritance chain
- unary/binary operator overloading
- virtual function and abstract class programs
- file read/write and file copy programs
Classes and objects
Bank account, array of objects
Constructors
Default, parameterised, copy
Inheritance
Publication/Book/Tape, Student/Exam/Result
Polymorphism
Function and operator overloading, virtual functions
File handling
Read, write and copy files
Topic 1
Reference variables and functions
A reference variable is an alias for an existing variable. Passing by reference lets a function change the caller's variables without pointers.
cppvoid swap(int &a, int &b) {
int t = a; a = b; b = t;
}
int main() {
int x = 5, y = 9;
swap(x, y);
cout << x << " " << y; // 9 5
}Topic 2
Bank account class and array of objects
cppclass Account {
string name; int number; double balance;
public:
void open(string n, int no, double b) { name = n; number = no; balance = b; }
void deposit(double a) { balance += a; }
void withdraw(double a) {
if (a > balance) cout << "Insufficient balance\n";
else balance -= a;
}
void display() { cout << number << " " << name << " " << balance << "\n"; }
};
Account acc[3]; // array of objects for multiple customersTopic 3
Function overloading: area of a triangle
Overload area() for (base, height) using ½ × b × h and for three sides using Heron's formula, s = (a + b + c)/2 and area = √[s(s − a)(s − b)(s − c)].
Topic 4
Inheritance programs
Book
adds page count
Tape
adds playing time in minutes
- Publication → Book and Tape (hierarchical): the base stores title and price; each derived class adds its own data and a display function.
- Student → Exam → Result (multilevel): Student stores roll number, Exam stores marks and Result calculates and displays the total.
Topic 5
Operator overloading, virtual functions and files
- Unary operator: overload
-to negate the coordinates of a point:void operator-() { x = -x; y = -y; } - Binary operator: overload
+to add two complex numbers or two distances. - Virtual function: a base
Shapewithvirtual void area(); derivedCircleandRectangleoverride it; call throughShape *. - Abstract class: make
area()pure virtual (= 0) and show thatShapeobjects cannot be created. - Files: write student records with
ofstream, read them withifstream, and copy a file withget()andput().
Exam tip
In the viva, expect: "Why use a reference instead of a pointer?", "What happens if a virtual keyword is removed?" and "Why can't we create an object of an abstract class?"
Key terms
- Reference variable
- An alias name for an existing variable
- Array of objects
- Several objects of one class stored in an array
- Heron's formula
- Area of a triangle from its three sides
- Pure virtual function
- A virtual function with = 0 and no body
Quick revision
- References change the caller's variables without pointer syntax.
- Withdraw must check for sufficient balance.
- Hierarchical: one base, many derived; multilevel: chain.
- Without virtual, a base pointer calls the base version.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What is a reference variable?
- Q2.How do you create an array of objects?
- Q3.What is Heron's formula?
- Q4.What changes if a function is not declared virtual?
- Q5.Which header is needed for file handling?
Long-answer questions
- Q1.Write a C++ program for a bank account class with deposit, withdraw and display, using an array of objects.
- Q2.Write a program to implement the Student–Exam–Result inheritance chain.
- Q3.Write a program to overload the + operator to add two complex numbers.
- Q4.Write a program to demonstrate virtual functions and an abstract class.
Stuck on this unit?
Message SBS on WhatsApp for help with Object Oriented Programming using C++ Laboratory, or to ask about studying BCA at Synetic.
