Unit 1 of 1 · BCA Sem 2

Unit 1: OOP implementation in C++

Object Oriented Programming using C++ Laboratory notes · PTU syllabus (UGSEC2504)

3 min read5 topics9 exam questions
On this page
  1. Unit summary
  2. Reference variables and functions
  3. Bank account class and array of objects
  4. Function overloading: area of a triangle
  5. Inheritance programs
  6. Operator overloading, virtual functions and files
  7. Key terms
  8. Quick revision
  9. Important questions

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
ClassificationLab programs mapped to OOP concepts
C++ lab
  • 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

1

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
}
2

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 customers
3

Topic 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)].

4

Topic 4

Inheritance programs

ClassificationPublication hierarchy
Publication|title, price
  • 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.
5

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 Shape with virtual void area(); derived Circle and Rectangle override it; call through Shape *.
  • Abstract class: make area() pure virtual (= 0) and show that Shape objects cannot be created.
  • Files: write student records with ofstream, read them with ifstream, and copy a file with get() and put().

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

  1. Q1.What is a reference variable?
  2. Q2.How do you create an array of objects?
  3. Q3.What is Heron's formula?
  4. Q4.What changes if a function is not declared virtual?
  5. Q5.Which header is needed for file handling?

Long-answer questions

  1. Q1.Write a C++ program for a bank account class with deposit, withdraw and display, using an array of objects.
  2. Q2.Write a program to implement the Student–Exam–Result inheritance chain.
  3. Q3.Write a program to overload the + operator to add two complex numbers.
  4. 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.

WhatsApp us