What is a Class? | C++ Advanced | Part – 2

what is class
classroom

What comes first in your mind when we think about class?

The above image? Yeah?

Let’s consider class is a group of girls and boys managed by a teacher.

class
class

Classes in C++ are data members and member functions bunched together.

Data members are nothing but the variables declared inside a class.

And, member functions are the functions that are used to access and manipulate these data members.

A class is a userdefined data type.

The members in the class can be accessed by creating the instance of the class. 

A class is a collection of data members and member functions.

Let’s see how we can write a class in C++.

Writing a Class

To declare a class, the keyword class is used.

class className{

Access Specifier:

Members

};

className is the name of the class.

Access Specifiers are the keywords that specify the access level of members. We will be learning about these things very soon.

And members are nothing but the data members and member functions.

Let’s write a class Student containing

  1. name and roll_number as data members.
  2. getInfo() as a member function which takes this information.
  3. And showInfo() another member function which prints the roll number and name of the student on the screen.
  4. We will be using public as the access specifier, we will learn about its significance shortly.

class Student{

public:

int roll_number;

string name;

void getInfo(int roll, string nam){

roll_number = roll;

name = nam;

}

void showInfo(){

cout<<“Name: “<<name<<”\nRoll Number: ”<<roll_number;

}

};

Here is our class just like the way we wanted.

What about accessing these member functions and assigning values to the data members?

Access specifiers are keywords which are used to restrict the scope of the data members and member functions.

Scope defines where these entities can be accessed and where they cannot.

There are three levels of access specifiers – Public, Protected, and Private.

Public

The members declared as public can be accessed anywhere.

Anywhere refers to outside the class and throughout the program.

To declare any members as public they must be written under the keyword – public.

Protected

The members declared as protected cannot be used outside the class.

They can only be accessed in the derived classes, i.e inherited classes.

We will be learning about derived classes shortly.

To declare any members as protected they must be written under the keyword – protected.

Private

The members of this can only be accessed inside the class.

The data members declared as private are accessed with the help of member functions of the class.

To declare any members as private they should be written under the keyword – private.

*Data members declared as public can be used throughout the program, anywhere

Objects in C++

Looks like someone’s waiting since long to learn how to access the members of the class.

The solution is – Object.

In Very Simple words Object is Car created from blueprint.

The Important thing to remember is Blueprint is class.

An object is a variable of type class.

Class is a user-defined data type.

An object is an instance of the class.

Objects are used to access and assign values to the members.

How?

Objects can be declared just like the way we declare any other variables.

Data type followed by variable name. Right?

So, in this case, our data type is the name of the class defined by us and the variable name is the name of the object.

Thus

className objectName;

is the syntax used to declare an object.

So, in order to declare an object student1 of class Student, we will write

 Student student1;

The correct syntax to declare an object is className ObjectName;

We can access the members of the class using a dot(.) operator.

 ObjectName.memberName;

Just like that.

Let’s access the member functions of the class Student which we previously wrote.

//Student Class

class Student{

public:

int roll_number;

string name;

void getInfo(int roll, string nam){

roll_number = roll;

name = nam;

}

void showInfo(){

cout<<“Name: “<<name<<“\nRoll Number: “<<roll_number;

}

};

int main(){

Student student1; //object of type student

student1.getInfo(45, “Rahul“); //accessing member function

student1.showInfo(); //accessing member function

}

Here, we have created the object student1 of type student.

We are accessing the member function getInfo() and passing values 45 and “Rahul” as arguments.

And then we are calling the function showInfo().

Can you guess the output of the above program?

The output will be

Name: Rahul Roll Number: 45

To Summarize

  • (dot operator) is used to access members of the class.
  • Data members declared as private can only be accessed inside the same class and by the member functions of the same class.
  • ObjectName.memberFunction(); is the correct way to access a member function declared inside a class.
  • Classes in C++ are data members and member functions bunched together.
  • A class is a user-defined data type.
  • To declare a class, the keyword class is used.
  •  class className{};
  • Access specifiers are keywords which are used to restrict the scope of the data members and member functions.
  • Objects are used to access and assign values to the members.

Leave a Reply

Your email address will not be published. Required fields are marked *