Unit 2 of 4 · BCA Sem 2

Unit 2: Classes, objects and constructors

Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Defining classes and member functions
  3. Access specifiers
  4. Array of objects
  5. Constructors
  6. Destructors
  7. Key terms
  8. Quick revision
  9. Important questions

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
ProcessLife of an object
  1. 1Declare the class

    Data members and member functions

  2. 2Create the object

    Constructor runs automatically

  3. 3Use the object

    Call member functions

  4. 4Object goes out of scope

    Destructor runs and frees resources

1

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();.

2

Topic 2

Access specifiers

SpecifierAccessible from
privateOnly member functions of the same class (default in a class)
publicAnywhere the object is visible
protectedThe class and its derived classes

Keeping data private and providing public functions to use it is called data hiding.

3

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.

4

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.

ComparisonTypes of constructors
Purpose
Example

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);
5

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

  1. Q1.What is the use of the scope resolution operator?
  2. Q2.Differentiate between private and public members.
  3. Q3.What is a constructor? List its characteristics.
  4. Q4.What is a copy constructor?
  5. Q5.What is a destructor?
  6. Q6.Can a constructor be overloaded?

Long-answer questions

  1. Q1.Explain how classes and objects are defined in C++ with an example.
  2. Q2.Explain the types of constructors with examples.
  3. Q3.Write a program using an array of objects to store and display details of bank accounts.
  4. 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.

WhatsApp us