In this article definition of class we give the information about A Class is a way to bind the data and its associated functions together. It allows the data member  and member function to be hidden, if necessary, from external use.

Definition of Class:

Class and Objects in C ++:

C ++ is an object-oriented programming language. Everything in C ++ is associated with class and object. That is, The program is written with the help of class and objects. An object is a logical entity as well as a physical entity whereas a class is only a logical entity.

Definition of Class:

” A Class is a way to bind the data and its associated functions together. It allows the data member  and member function to be hidden, if necessary, from external use. “

Class in C ++

  • In C ++, a class is a prototype defined by a user.
  • A class is a group of identical objects.
  • A class is a blueprint for the object.
  • It has its own data members and member functions which are used by creating instance of class.

For example: Suppose we have a class named Cars. And it has cars of different names and brands. But all these cars will have some common properties. Such as – four wheels, mileage, speed etc. That’s why Cars is a class here and four wheels, mileage, speed are its properties.

Definition of Class:

Creating class:

The class keyword is used to create a class. And after this the name of the class is written. The first character of the class name can also be both Uppercase or Lowercase. The body of the class is written inside curly braces { } followed by semicolon( ; ) is applied.

There are some more important parts to the class. For example,

Access Specifier:  Access Specifier / Modifier is used to access Access Control.

There are three types of Access Specifier.

  1. private
  2. public
  3. protected

1 Private:

In private, class members mean variables are written. Without private, members of private are written as default private member. private member works only for his class. They are not accessible outside the class.

2. Protected:

Protected work is similar to private. They are inherited. They are used in Inheritance.

3. Public:

Members of public are accessed both inside and outside the class. Data members are also written inside it.

Class syntax:

class class_name

{

      private:

                  Data member(s);

                  Member function(s);

      public:

                  Data member(s);

                  Member function(s);

};

For example

In the following example, the class name ‘Book’ and curly brace are open ({) with ‘class’ keyword. With this data members and member function is defined. After this, semicolon (;) is finally given by curly brace close (}).

Class Book

{

             private:

                           int p; // data member

             public:

                           void getdata (); // member function

};

Example of –

class rect

{

                 public:

                             int length, breadth;

                             int RectArea ()

                             {

                                            return length * breadth;

                             }

};

The class name in the example above is Rect. It has length and breadth variables, they are also called data members. It has a function RectArea () which is also called as member function.

Objects in C ++:

  • object is an instance (example) of a class. All members of the class can be accessed by object.
  • It is a real-world entity. Such as – chair, pen, book, mobile, laptop etc.
  • In other words, “object is an entity whose state and behavior are.” Here state means data and behavior means functionality.
  • object is a run-time entity. It is created in runtime. Objects are like variables.
  • The class for object is taken as a data type and is given one or more variables (objects).
  • The object of the class is written inside the main () function.
  • The member functions of the class are accessed with the object and access operator.

Object in C++:

Definition:

“In C++, the class variables are called objects.  Or object is basic run time entity in OOP”

With objects we can access the public members of a class using the dot(.) operator.

Syntax: –

ClassName ObjectName;

In the example of Rect given above – We can create the object of Rect class as follows.

int main ()

{

// create objects

Rect r1, r2;

}

Here we have created two objects named r1, r2 in main ().

Accessing Data Members:

We can access the public data members of the class using the dot (.) operator along with the object.

Accessing Class Members

The object and member function are used to access the data of the class. With this, access operator (.) Is required.

Syntax for Accessing Class Members

object_name.member_function ();

For example:

r1.getdata ();

example

class Stud

{

public:

                int rollno;

                string name;

};

int main()

{

                Stud A;

                Stud B;

   // setting values for A object

                A.rollno=10;

                A.name=”Rajveer”;

   // setting values for B object

                 B.rollno=11;

                 B.name=”Bhagyashri”;

                 cout <<“Name and Roll no of A is: “<< A.name << “-” << A.rollno;

                 cout <<“Name and Roll no of B is: “<< B.name << “-” << B.rollno;

}

OutPut:–

Name and Roll no of A is: Rajveer-10
Name and Roll no of B is: Bhagyashri-11

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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