Unit 2: Classes, objects and constructors
Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)
On this page
Unit summary
Classes and objects are the heart of C++. This unit shows how to define a class with data members and member functions, control access with private, public and protected, create arrays of objects, and initialise and clean up objects using constructors and destructors.
After this unit you can
- Define classes and member functions inside and outside the class
- Use access specifiers to hide data
- Create arrays of objects
- Write default, parameterised, copy and multiple constructors and a destructor
PTU syllabus topics
- Defining classes and member functions
- declaring objects
- access specifiers (private/public/protected)
- array of objects
- parameterized and copy constructors
- multiple constructors
- dynamic initialization
- destructors
Topic 1
Defining classes and member functions
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();.
Example
Output of a member-function call: class Account has balance = 1000. After a1.deposit(500); a1.show(); the balance is 1500. A second object a2 has its own balance — depositing into a1 does not change a2, because every object keeps its own copy of the data members, while the member functions are shared.
Topic 2
Access specifiers
Encapsulation: private data is reached only through public member functions
| 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 3
Array of 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.
Exam tip
Writing a class in an exam: (1) the keyword class and the name, (2) data members under private, (3) member functions under public, (4) the closing semicolon after the last brace, and (5) a short main() that creates an object and calls a function. Missing the semicolon is the most common compile error.
Topic 4
Constructors
The constructor initialises members when the object is created; the destructor frees resources when it goes out of scope
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).
Example
Which constructor runs? With the Point class below, Point p1; calls the default constructor (x = y = 0); Point p2(3, 4); calls the parameterised constructor; Point p3(p2); calls the copy constructor, so p3 is also (3, 4). Order of destruction at the end of the program is the reverse of creation: p3, then p2, then p1.
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 5
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
- Data member
- A variable declared inside a class
- Member function
- A function declared inside a class
- Scope resolution operator
- :: used to define members outside the class
- Constructor
- A special function that initialises an object
- Destructor
- A special function called when an object is destroyed
Quick revision
- Class members are private by default.
- Define outside with ClassName::function.
- Constructor: same name as class, no return type, called automatically.
- Copy constructor takes a reference to an object of the same class.
- Destructor: ~ClassName(), one per class, no arguments.
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 the use of the scope resolution operator?
- Q2.Differentiate between private and public members.
- Q3.What is a constructor? List its characteristics.
- Q4.What is a copy constructor?
- Q5.What is a destructor?
- Q6.Can a constructor be overloaded?
Long-answer questions
- Q1.Explain how classes and objects are defined in C++ with an example.
- Q2.Explain the types of constructors with examples.
- Q3.Write a program using an array of objects to store and display details of bank accounts.
- Q4.Differentiate between constructors and destructors and explain both with a program.
Stuck on this unit?
Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying BCA at Synetic.
