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
- 1Declare the class
Data members and member functions
- 2Create the object
Constructor runs automatically
- 3Use the object
Call member functions
- 4Object goes out of scope
Destructor runs and frees resources
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();.
Topic 2
Access specifiers
| 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.
Topic 4
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 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.
