Study resource

Read at your pace, then save it for later.

Unit 6: Classes and Objects in C++

Introduction

In object-oriented programming (OOP), a class is a user-defined blueprint or prototype from which objects are created. It groups data and methods that manipulate that data into a single unit. An object is an instance of a class that exists physically in memory during program execution.

Specifying a Class

To specify a class in C++, you use the class keyword followed by the class name. The body of the class is enclosed in curly braces and must be terminated with a semicolon.

Class Specification Syntax:
class ClassName {
  private:
    // Private data members
    // Private member functions
  public:
    // Public data members
    // Public member functions
};

Access Specifiers

Access specifiers define how the members (variables and functions) of a class can be accessed from outside the class. C++ provides three types of access specifiers:

  • private: Members declared as private can only be accessed by the member functions of the same class. This is the default access specifier for C++ classes.
  • public: Members declared as public can be accessed from anywhere outside the class where the class object is visible.
  • protected: Members declared as protected cannot be accessed from outside the class, but they can be accessed by any derived (child) class in inheritance.

Comparison Table of Access Specifiers

Access SpecifierWithin Same ClassWithin Derived ClassOutside Class
privateYesNoNo
protectedYesYesNo
publicYesYesYes

Defining Member Functions

Member functions of a class can be defined in two ways: inside the class definition or outside the class definition.

1. Inside the Class Definition

When a member function is defined inside the class, it is automatically treated as an inline function by the compiler if possible. This is ideal for small, simple functions.

Example:
class Circle {
  public:
    double radius;
    double getArea() {
        return 3.14159 * radius * radius;
    }
};

2. Outside the Class Definition

To define a member function outside the class, you must first declare its prototype inside the class definition and then define it outside using the scope resolution operator (::).

Syntax:
return_type ClassName::function_name(parameters) {
    // Function body
}
Example:
class Circle {
  public:
    double radius;
    double getArea(); // Prototype declaration
};

// Definition outside the class
double Circle::getArea() {
    return 3.14159 * radius * radius;
}

Nesting of Member Functions

Nesting of member functions refers to the process where a member function of a class calls another member function of the same class directly without using the dot (.) operator. This is useful when a complex operation needs to be split into smaller helper functions, which are often kept private to enforce encapsulation.

Example:
class Cuboid {
  private:
    double length, width, height;
    double getBaseArea() {
        return length * width;
    }
  public:
    void setDimensions(double l, double w, double h) {
        length = l;
        width = w;
        height = h;
    }
    double getVolume() {
        // Nesting: calling getBaseArea() directly without dot operator
        return getBaseArea() * height;
    }
};

A Sample C++ Program with Class

The following complete C++ program demonstrates all of the concepts described above, including class specification, access specifiers, member function definitions (inside and outside), and nesting of member functions.

#include <iostream>
using namespace std;

class Student {
  private:
    int rollNo;
    float marks;

    // Private helper function for nesting
    char calculateGrade() {
        if (marks >= 90.0) return 'A';
        else if (marks >= 75.0) return 'B';
        else return 'C';
    }

  public:
    // Defined inside the class
    void setDetails(int r, float m) {
        rollNo = r;
        marks = m;
    }

    // Declared inside, to be defined outside
    void displayDetails();
};

// Defining member function outside using the scope resolution operator
void Student::displayDetails() {
    cout << "Roll Number: " << rollNo << endl;
    cout << "Marks: " << marks << endl;
    
    // Nesting of member function: calling private calculateGrade() directly
    cout << "Grade: " << calculateGrade() << endl;
}

int main() {
    Student s1; // Creating an object
    
    s1.setDetails(101, 88.5); // Accessing public member function
    s1.displayDetails();
    
    return 0;
}

Important Exam Observations

  • Semicolon: Always remember to terminate the class definition with a semicolon (;). Forgetting this causes a common compiler syntax error.
  • Default Private Status: If you omit the access specifier, all variables and methods in a C++ class are private by default.
  • Dot Operator: Use the dot operator (.) to access public members of a class through an object inside non-member functions like main(). Do not use the dot operator when nesting member functions within the class.

xxx

Did this help you understand better?

Your feedback improves the quality of this resource for everyone.