Unit 1: OOP implementation in C++
Software Lab-III (OOPs using C++) notes · PTU syllabus (BSIT205/BSBC206)
On this page
- Unit summary
- Structure, union and class
- Inline, static and friend functions
- Objects and arrays of objects
- Constructors and destructors
- Function overloading
- Inheritance with public, private and protected visibility
- Operator overloading, virtual functions and file streams
- Key terms
- Quick revision
- 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
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
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;
}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";
}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 customersTopic 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.
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)].
Topic 6
Inheritance with public, private and protected visibility
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 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
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
- 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
- Q1.Why does a static data member need a definition outside the class?
- Q2.Can a friend function access private members? Why?
- Q3.When is the copy constructor invoked?
- Q4.How is the derived class constructor called?
- Q5.Which operator function overloads +?
- Q6.How do you open a file in append mode in C++?
Long-answer questions
- Q1.Write a program using static and friend functions.
- Q2.Write a program demonstrating default, parameterised and copy constructors and a destructor.
- Q3.Write a program to implement multilevel inheritance.
- Q4.Write a program to overload the + operator for complex numbers.
- 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.
