Unit 3: Classes, objects and constructors
Object Oriented Programming using C++ notes · PTU syllabus (PGCA1911)
On this page
- Unit summary
- Class definition
- Member functions and the scope resolution operator
- Private, protected and public members
- Nesting of member functions
- Creating and accessing objects
- Parameterised and copy constructors
- Multiple constructors and dynamic initialisation
- Destructors
- Key terms
- Quick revision
- Important questions
Unit summary
Classes bundle data with the functions that use it, and constructors set objects up correctly. This unit covers class and member function definitions, the scope resolution operator, private, protected and public members, nesting of member functions, creating and accessing objects, parameterised and copy constructors, multiple constructors, dynamic initialisation and destructors.
After this unit you can
- Define classes with member functions inside and outside
- Apply access specifiers and nesting of member functions
- Create and use objects
- Write constructors and destructors
PTU syllabus topics
- Class and function definitions
- scope resolution operator
- private/protected/public members
- nesting of member functions
- object creation and access
- parameterized and copy constructors
- multiple constructors
- dynamic initialization
- destructors
Default
No arguments
Box b;
Parameterised
Takes values
Box b(2, 3);
Copy
Copies another object
Box c(b);
Destructor
Releases resources
~Box()
Topic 1
Class definition
A class has data members (variables) and member functions (methods). Member functions can be defined inside the class or outside using the scope resolution operator ::.
cppclass Account {
private:
int number;
double balance;
public:
void deposit(double amt); // declared here
void show() { cout << number << " " << balance; }
};
void Account::deposit(double amt) { // defined outside
balance += amt;
}Objects are declared like variables: Account a1, a2; and members are accessed with the dot operator: a1.show();.
Topic 2
Member functions and the scope resolution operator
cppclass Rectangle {
int l, b;
public:
void set(int x, int y) { l = x; b = y; } // defined inside: implicitly inline
int area(); // declared only
};
int Rectangle::area() { return l * b; } // defined outside using ::- Scope resolution operator :: links a function to its class, accesses global variables hidden by locals and static members.
Topic 3
Private, protected and public members
| Specifier | Accessible from |
|---|---|
| private | Only member functions of the same class (default in a class) |
| public | Anywhere the object is visible |
| protected | The class and its derived classes |
Keeping data private and providing public functions to use it is called data hiding.
Topic 4
Nesting of member functions
- A member function may call another member function of the same class directly by name, without an object or dot operator.
cppclass Marks {
int m1, m2;
int total() { return m1 + m2; } // private helper
public:
void read() { cin >> m1 >> m2; }
void show() { cout << "Total " << total() << ", Average " << total() / 2.0; } // nesting
};Topic 5
Creating and accessing objects
An array of objects stores many objects of the same class: Account acc[10]; Each element is accessed as acc[i].show();.
Example
A bank program creates Account acc[3]; and loops through them to deposit money and display each balance.
Topic 6
Parameterised and copy constructors
A constructor is a special member function with the same name as the class and no return type, called automatically when an object is created. It initialises the object.
Default
Takes no arguments
Account() { balance = 0; }
Parameterised
Takes arguments to initialise
Account(int n, double b)
Copy
Creates an object as a copy of another
Account(const Account &a)
Multiple (overloaded)
Several constructors in one class
Default and parameterised together
Dynamic initialisation means passing values to a constructor at run time (for example values read with cin).
cppclass Point {
int x, y;
public:
Point() { x = y = 0; } // default
Point(int a, int b) { x = a; y = b; } // parameterised
Point(const Point &p) { x = p.x; y = p.y; } // copy
};
Point p1, p2(3, 4), p3(p2);Topic 7
Multiple constructors and dynamic initialisation
cppclass Account {
long accNo; double balance, rate;
public:
Account() : accNo(0), balance(0), rate(3.5) {} // default
Account(long n, double b) : accNo(n), balance(b), rate(3.5) {} // two arguments
Account(long n, double b, double r) : accNo(n), balance(b), rate(r) {}
void show() { cout << accNo << ' ' << balance << ' ' << rate << '\n'; }
};
int main() {
long n; double b;
cin >> n >> b;
Account a1, a2(101, 5000), a3(n, b, 4.0); // dynamic initialisation: values known only at run time
a3.show();
}- Constructor overloading lets objects be created in different ways; dynamic initialisation passes run-time values to constructors; the member initialiser list initialises members efficiently.
Topic 8
Destructors
A destructor has the class name preceded by a tilde (~Point()), takes no arguments, has no return type and is called automatically when an object goes out of scope. It releases resources such as memory allocated with new. A class can have only one destructor.
Exam tip
A frequent short question: constructors can be overloaded; destructors cannot.
Key terms
- Class
- User-defined type combining data and functions
- Scope resolution operator
- :: used to define members outside a class
- Constructor
- Member function called automatically when an object is created
- Copy constructor
- Constructor creating an object from another of the same class
- Destructor
- Member function called when an object is destroyed
Quick revision
- class syntax; members defined inside (inline) or outside with ::.
- private, protected, public; data hiding.
- Nesting of member functions; objects and the dot operator; arrays of objects.
- Default, parameterised, copy, multiple constructors; initialiser lists; dynamic initialisation.
- Destructor with ~; order of destruction is reverse of creation.
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.How is a member function defined outside a class?
- Q2.Distinguish private and protected members.
- Q3.What is nesting of member functions?
- Q4.When is a copy constructor called?
- Q5.What is dynamic initialisation of objects?
- Q6.Can a destructor be overloaded?
Long-answer questions
- Q1.Explain classes and objects with a program.
- Q2.Explain access specifiers and nesting of member functions.
- Q3.Explain types of constructors with examples.
- Q4.Explain constructor overloading, dynamic initialisation and destructors.
Stuck on this unit?
Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying M.Sc IT at Synetic.
