Unit 3 of 4 · M.Sc IT Sem 2

Unit 3: Classes, objects and constructors

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

3 min read8 topics10 exam questions
On this page
  1. Unit summary
  2. Class definition
  3. Member functions and the scope resolution operator
  4. Private, protected and public members
  5. Nesting of member functions
  6. Creating and accessing objects
  7. Parameterised and copy constructors
  8. Multiple constructors and dynamic initialisation
  9. Destructors
  10. Key terms
  11. Quick revision
  12. 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
ComparisonTypes of constructors
Purpose
Example

Default

No arguments

Box b;

Parameterised

Takes values

Box b(2, 3);

Copy

Copies another object

Box c(b);

Destructor

Releases resources

~Box()

1

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

2

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.
3

Topic 3

Private, protected and public members

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.

4

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

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.

6

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.

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

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.
8

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

  1. Q1.How is a member function defined outside a class?
  2. Q2.Distinguish private and protected members.
  3. Q3.What is nesting of member functions?
  4. Q4.When is a copy constructor called?
  5. Q5.What is dynamic initialisation of objects?
  6. Q6.Can a destructor be overloaded?

Long-answer questions

  1. Q1.Explain classes and objects with a program.
  2. Q2.Explain access specifiers and nesting of member functions.
  3. Q3.Explain types of constructors with examples.
  4. 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.

WhatsApp us