Classes and Objects Notes for Class 12(ASSEB/AHSEC)

Chapter 3 - Classes and Objects in C++

Introduction

This section covers the basic concepts of Classes and Objects. Here we shall learn what is a class, class declaration, members of a class – data members and member functions, access specifiers – private, protected and public , objects , declaration of objects, nested classes, static class members

Classes and Objects

Classes and objects are the foundation of Object-Oriented Programming (OOP) in C++. They allow us to combine data and the functions that work on that data into a single unit.

Before learning advanced OOP concepts such as inheritance and polymorphism, it is important to understand how classes and objects work.

What is a Class?

Definition

A class is a user-defined data type that groups together data members and member functions.In simple words, a class acts like a blueprint or design for creating objects.

Easy Explanation

Imagine a company that manufactures cars.

Before making the cars, engineers prepare a design (blueprint).

Using this one blueprint, thousands of cars can be manufactured.

Similarly:

  • Blueprint = Class
  • Cars = Objects

The class describes what an object should contain

Example

Suppose we want to store information about a student. A student has data such as name, roll number and marks. We can also have functions such as display() to display the student’s information.

class Student

{

    int roll;

    float marks;

    void display()

    {

        cout << roll << ” ” << marks;

    }

};

Here, Student is a class.

The class describes what information a student object will contain and what operations can be performed on it.

Important Point

A class is only a blueprint. It does not occupy memory. Memory is allocated only when objects of the class are created

Class Declaration

Syntax:

class class-name {

             private:

                            [data members]

                             [member function]

              protected:

                               [data members]

                               [member function]

                public:

                                [data members]

                               [member function]

                                    };

Here class is a keyword

Class-name is name given to class

Private, protected and public are called access specifiers

Data members and member functions are class members

Members of a Class

The elements declared inside a class are called members of the class or class members.

There are mainly two types of class members:

  1. Data Members
  2. Member Functions
class members

Data Members

The variables declared inside a class are called data members.They represent the characteristics or properties of an object

Example

Student

Data Members:

  • Name
  • Roll Number
  • Marks
  • Address

Car

Data Members:

  • Brand
  • Colour
  • Speed
  • Price

Example:

class Student

{

    int roll;

    char name[30];

    float marks;

};

Here:

  • roll is a data member.
  • name is a data member.
  • marks is a data member.

Member Functions

Functions declared inside a class are called member functions.They are used to perform operations on the data members of the class.

Example:

class Student

{

    int roll;

    float marks;

 

    void display()

    {

        cout << roll << ” ” << marks;

    }

};

Here, display() is a member function.

Remember

Data members store information, while member functions perform operations on that information.

Access Specifiers

C++ provides access specifiers to control how the members of a class can be accessed.

The three access specifiers are:

  • private
  • protected
  • public

They help us control the visibility of class members.

Access specifiers

Private Members

A member declared as private can normally be accessed only by the member functions of the same class. They are hidden from the outside world.

Example:

class Student

{

private:

    int roll;

 

public:

    void setRoll(int r)

    {

        roll = r;

    }

 

    void showRoll()

    {

        cout << roll;

    }

};

Here, roll is private. It cannot normally be accessed directly using an object from outside the class.

Student s;

// s.roll = 10;   // Error

Instead, we use a public member function such as setRoll().

Public Members

Members declared as public can be accessed from outside the class using an object.

Example:

class Student

{

public:

    int roll;

};

Now we can write:

Student s;

s.roll = 10;

 

cout << s.roll;

Protected Members

A protected member is similar to a private member in that it cannot normally be accessed directly from outside the class.

The important difference is that protected members can also be accessed by derived classes.

This becomes especially useful when we study inheritance

Example:

class Student

{

protected:

    int roll;

};

A derived class can use roll, but an ordinary object cannot directly access it.

Default Access Specifier

If no access specifier is written in a C++ class, the members are private by default.

Example:

class Student

{

    int roll;

    float marks;

};

Both roll and marks are private.

This is equivalent to:

class Student

{

private:

    int roll;

    float marks;

};

Important Exam Point

Default access specifier of a class in C++ is private.

scope of access specifier

Defining Member Functions

A member function can be defined in two ways:

  1. Inside the class definition
  2. Outside the class definition

Member Function Defined Inside the Class

A member function can be written directly inside the class.

Example

class Student

{

    int roll;

 

public:

    void show()

    {

        cout << roll;

    }

};

Here, show() is defined inside the class.

Functions defined inside a class definition are treated as inline functions by the compiler, subject to the usual rules governing inline functions

Member Function Defined Outside the Class

A member function can also be declared inside the class and defined outside it.

The scope resolution operator :: is used to tell the compiler that the function belongs to a particular class.

Example

class Student

{

    int roll;

public:

    void show();

};

void Student::show()

{

    cout << roll;

}

Here:

void Student::show()

means that show() is a member function of the Student class.

Scope Resolution Operator ::

The scope resolution operator is used to specify the scope to which a member belongs.

ClassName::functionName()

Example:

Student::show()

Inline Functions

An inline function is a function for which the compiler may replace the function call with the actual function code, where appropriate.

This can reduce the overhead of a function call for very small functions.

Example:

class Number

{

public:

    inline int square(int n)

    {

        return n * n;

    }

};

The keyword inline can be used explicitly.

inline int square(int n)

{

    return n * n;

}

Important Point

The inline keyword is a request to the compiler, not a guarantee that the function will always be expanded inline.

Inline functions are generally useful for small functions that are called frequently.

Objects

An object is an instance of a class.

If a class is considered a blueprint, an object is an actual entity created using that blueprint.

Example:

class Student

{

public:

    int roll;

};

 

Student s1;

Student s2;

Here:

  • Student is a class.
  • s1 is an object of Student.
  • s2 is another object of Student.

Each object has its own copy of the class’s non-static data members

Declaration of Objects

Objects can be declared after defining a class.

Example:

class Student

{

public:

    int roll;

};

Student s1;

Student s2;

Multiple objects can also be declared in one statement:

Student s1, s2, s3;

The objects s1, s2 and s3 are separate instances of the Student class.

Accessing Class Members Using Objects

Public members of a class can be accessed using an object and the dot (.) operator.

Example:

class Student

{

public:

    int roll;

    void display()

    {

        cout << roll;

    }

};

int main()

{

    Student s;

    s.roll = 25;

    s.display();

    return 0;

}

Here:

s.roll = 25;

accesses the data member roll.

And:

s.display();

calls the member function display().

Important Point

The dot operator . is commonly used to access public members through an object.

Global and Local Objects

Objects can be declared at different places in a program.

Depending on where an object is declared, it may be a global object or a local object.

Global Object

An object declared outside all functions is called a global object.

Local Object

An object declared inside a function or block is called a local object.

Nesting of Member Functions

A member function can call another member function of the same class.

This is commonly referred to as nesting of member functions.

Nested Class

A nested class is a class declared inside another class.

Objects as Function Arguments

An object can be passed to a function just like other data types.

There are two common ways of passing an object to a function:

  1. Pass by value
  2. Pass by reference

Passing Object by Value

When an object is passed by value, a copy of the object is passed to the function.

Changes made to the parameter inside the function do not change the original object.

Passing Object by Reference

An object can also be passed by reference.Here reference to the original object is passed.The function works with the original object.Changes made affect the original object

Static Class Members

Normally, each object has its own copy of non-static data members.

Sometimes, however, we want a particular piece of data to be shared by all objects of a class.

For this purpose, C++ provides static members.

Static class members are mainly of two types:

  1. Static data members
  2. Static member functions

Static Data Member

A static data member is shared by all objects of a class.

Unlike an ordinary data member, only one shared copy exists for the class.

.

Static Member Function

A static member function belongs to the class rather than to a particular object.

It can be called using the class name.

Quick Revision

Scroll to Top