Unit 1 of 1 · B.Sc IT Sem 2

Unit 1: OOP implementation in C++

Software Lab-III (OOPs using C++) notes · PTU syllabus (BSIT205/BSBC206)

3 min read7 topics11 exam questions
On this page
  1. Unit summary
  2. Structure, union and class
  3. Inline, static and friend functions
  4. Objects and arrays of objects
  5. Constructors and destructors
  6. Function overloading
  7. Inheritance with public, private and protected visibility
  8. Operator overloading, virtual functions and file streams
  9. Key terms
  10. Quick revision
  11. Important questions

Unit summary

This lab implements OOPs using C++: structures, unions and classes, member functions defined inside and outside the class, inline, static and friend functions, objects and arrays of objects, constructors and destructors, inheritance with different visibility modes, function and operator overloading, early and late binding with virtual functions, and file streams. Each section gives model programs and viva points.

After this unit you can

  • Write classes with member, inline, static and friend functions
  • Use constructors, destructors and arrays of objects
  • Implement inheritance, overloading and virtual functions
  • Read and write files using streams

PTU syllabus topics

  • Structure/union/class definition and declaration
  • class function definitions (inside/outside class, inline, static, friend)
  • creating objects
  • arrays of objects
  • default/parameterized/copy constructors and destructors
  • base/derived classes with public/private/protected visibility
  • single/multi-level/multiple inheritance
  • function and operator overloading
  • early/late binding
  • virtual and pure virtual functions
  • input/output file streams
ClassificationC++ lab programs at a glance
Lab exercises
  • Classes and objects

    Members, inline, static, friend

  • Constructors and destructors

    Default, parameterised, copy

  • Inheritance

    Single, multilevel, multiple

  • Overloading

    Functions and operators

  • Polymorphism and files

    Virtual functions, fstream

1

Topic 1

Structure, union and class

cpp#include <iostream>
using namespace std;
struct S { int a; };                 // members public by default
union U { int i; float f; };          // members share memory
class C { int x; public: void set(int v) { x = v; } int get() { return x; } };
int main() {
    S s{5}; U u; u.i = 10; C c; c.set(7);
    cout << s.a << " " << u.i << " " << c.get() << " " << sizeof(U) << endl;
}
2

Topic 2

Inline, static and friend functions

cppclass Student {
    static int count;
    int marks;
public:
    Student(int m) : marks(m) { ++count; }
    inline int getMarks() const { return marks; }
    static int total() { return count; }
    friend void compare(const Student &a, const Student &b);
};
int Student::count = 0;
void compare(const Student &a, const Student &b) {
    cout << (a.marks > b.marks ? "First" : "Second") << " is higher\n";
}
3

Topic 3

Objects and arrays 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
4

Topic 4

Constructors and destructors

cppclass Box {
    int l, b;
public:
    Box() : l(1), b(1) { cout << "Default\n"; }
    Box(int x, int y) : l(x), b(y) { cout << "Parameterised\n"; }
    Box(const Box &o) : l(o.l), b(o.b) { cout << "Copy\n"; }
    ~Box() { cout << "Destructor\n"; }
    int area() { return l * b; }
};
int main() { Box a; Box b(3, 4); Box c = b; cout << c.area() << endl; }
  • Viva: constructors have the class name and no return type; destructors run in reverse order of creation.
5

Topic 5

Function overloading

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

6

Topic 6

Inheritance with public, private and protected visibility

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

Topic 7

Operator overloading, virtual functions and file streams

  • 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

Inline function
Function expanded at the call site
Static member
Member shared by all objects
Copy constructor
Constructor initialising an object from another
Virtual function
Function resolved at run time through a base pointer
ofstream
Output file stream class

Quick revision

  • struct public by default; union shares memory; class private by default.
  • Inline, static (shared counter), friend (access to private data).
  • Default, parameterised, copy constructors; destructor order.
  • Overloading by parameters; inheritance modes; operator functions.
  • Virtual and pure virtual functions; ifstream, ofstream, fstream.

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.Why does a static data member need a definition outside the class?
  2. Q2.Can a friend function access private members? Why?
  3. Q3.When is the copy constructor invoked?
  4. Q4.How is the derived class constructor called?
  5. Q5.Which operator function overloads +?
  6. Q6.How do you open a file in append mode in C++?

Long-answer questions

  1. Q1.Write a program using static and friend functions.
  2. Q2.Write a program demonstrating default, parameterised and copy constructors and a destructor.
  3. Q3.Write a program to implement multilevel inheritance.
  4. Q4.Write a program to overload the + operator for complex numbers.
  5. Q5.Write a program using virtual functions and file streams.

Stuck on this unit?

Message SBS on WhatsApp for help with Software Lab-III (OOPs using C++), or to ask about studying B.Sc IT at Synetic.

WhatsApp us