Unit 2 of 4 · B.Sc IT Sem 2

Unit 2: Class functions and constructors

OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)

3 min read6 topics10 exam questions
On this page
  1. Unit summary
  2. Member functions inside and outside the class
  3. Friend, inline and static members
  4. Creating objects and arrays of objects
  5. Passing objects by value and by reference
  6. Default, parameterised and copy constructors
  7. Destructors
  8. Key terms
  9. Quick revision
  10. Important questions

Unit summary

Member functions, special functions and constructors make classes useful. This unit covers defining member functions inside and outside the class, friend, inline and static functions and members, the scope resolution operator, creating and accessing objects, arrays of objects, passing objects by value and reference, default, parameterised and copy constructors, and destructors.

After this unit you can

  • Define member functions inside and outside the class
  • Use friend, inline and static functions and members
  • Create objects, arrays of objects and pass objects to functions
  • Use constructors and destructors

PTU syllabus topics

  • Member function definitions inside/outside the class
  • friend functions
  • inline functions
  • static members and functions
  • scope resolution operator
  • creating and accessing objects
  • arrays of objects
  • pass by value/reference
  • default/parameterized/copy constructors
  • destructors
ComparisonTypes of constructors
Purpose
Example

Default

Creates an object with no arguments

Student s;

Parameterised

Initialises with given values

Student s(1, "Ana");

Copy

Creates a copy of another object

Student t(s);

Destructor

Cleans up when the object is destroyed

~Student()

1

Topic 1

Member functions inside and outside the class

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

Topic 2

Friend, inline and static members

  • Friend function: a non-member function given access to private members — declared with the keyword friend inside the class; useful for operations on two classes.
  • Inline function: the compiler may replace a call with the function body to save call overhead — for small, frequently called functions.
  • Static data member: one copy shared by all objects (e.g., a counter); defined outside the class. Static member function: accesses only static members; called with ClassName::function().
cppclass Account {
    static int count;                 // shared by all objects
    double balance;
public:
    Account() { ++count; balance = 0; }
    static int total() { return count; }
    friend void audit(const Account &a);
};
int Account::count = 0;
void audit(const Account &a) { std::cout << a.balance; }   // friend accesses private data
3

Topic 3

Creating objects and arrays 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

Passing objects by value and by reference

  • By value: a copy is passed — changes inside the function do not affect the caller; the copy constructor is invoked.
  • By reference: the function works on the original object (use const reference to avoid copying when no change is needed).
cppvoid raise(Employee e)  { e.salary += 1000; }   // caller unchanged
void raise(Employee &e) { e.salary += 1000; }   // caller changed
5

Topic 5

Default, 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);
6

Topic 6

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

Scope resolution operator
:: operator linking names to a scope
Friend function
Non-member function with access to private members
Inline function
Function expanded at the point of call
Static member
Member shared by all objects of a class
Copy constructor
Constructor creating an object from another object

Quick revision

  • Inside (implicitly inline) and outside (::) definitions.
  • Friend, inline, static members and functions.
  • Objects and arrays of objects.
  • Pass by value vs reference (const reference).
  • Default, parameterised, copy constructors; destructors (~).

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 scope resolution operator used for?
  2. Q2.What is a friend function?
  3. Q3.When should a function be inline?
  4. Q4.What is a static data member?
  5. Q5.When is a copy constructor called?
  6. Q6.What is a destructor?

Long-answer questions

  1. Q1.Explain member functions defined inside and outside a class.
  2. Q2.Explain friend, inline and static functions with examples.
  3. Q3.Explain arrays of objects and passing objects to functions.
  4. Q4.Explain the types of constructors and destructors with examples.

Stuck on this unit?

Message SBS on WhatsApp for help with OOPs using C++, or to ask about studying B.Sc IT at Synetic.

WhatsApp us