banner



how to create a class in c++

In this section, you will learn to make our own class. Before going further, let's look at an example:

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              public              :              int              length              ;              //length of rectangle              int              breadth              ;              //breadth of rectangle              /* declaring member functions */              void              setLength              (              int              l              );              void              setBreadth              (              int              b              );              int              getArea              ();              };              /* defining member functions */              void              Rectangle              ::              setLength              (              int              l              )              {              length              =              l              ;              }              void              Rectangle              ::              setBreadth              (              int              b              )              {              breadth              =              b              ;              }              int              Rectangle              ::              getArea              ()              {              return              length              *              breadth              ;              }              int              main              ()              {              Rectangle              rt              ;              rt              .              setLength              (              7              );              rt              .              setBreadth              (              4              );              int              area              =              rt              .              getArea              ();              cout              <<              "Area : "              <<              area              <<              endl              ;              return              0              ;              }            

Output

Let's go through this code to understand how to make our own class.

Remember that the execution of any program begins with the main function. So while executing, the first statement of the main function will get executed first.

Rectangle rt; - This statement declares rt as an object of class Rectangle. It is the same as saying that 'rt' is a 'Rectangle'. For this, 'Rectangle' must be defined.

class Rectangle - We have defined our own class named 'Rectangle'. 'class' is a keyword which means that 'Rectangle' is a class.

Inside the Rectangle class, we declared two variables and three functions. These variables and functions belong to the class Rectangle since these are declared inside the class and thus are called members of the class. There are two types of members of a class:

  • data members - In the class Rectangle, length and breadth are the data members since these store the information ( length and breadth ) of the objects of the class.
  • member functions - setLength(), setBreadth and getArea() are the member functions of the Rectangle class.

Note that while defining the member functions, we have written Rectangle:: before the function name. This is to tell the compiler that the function belongs to the class Rectangle.

rt.setLength(7); - This statement will call the function setLength with the parameter value 7. To call any function, we use (.) DOT after the object and then call that function.
Since 'rt' is an object of the 'Rectangle' class, so 'rt.setLength' will call the function 'setLength' of 'Rectangle' class for 'rt'. This will set the value of length as 7 for 'rt'

Similarly, rt.setBreadth(4) will call the function setBreadth and will set the value of breadth as 4.

int area = rt.getArea(); - 'rt' will call the function getArea() which will return length * breadth which is 28 (since the value of length is 7 and that of braedth is 4). This value will get assigned to the variable area.

public: - We declared all the members of the class as public. public is a modifier which allows the members of a class to be accessed directly from outside the class.

Like public, there are other modifiers also like private and protected. We will study more about modifier later.

We can also define our member methods at the time it is declared in the function as in the following example.

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              public              :              int              length              ;              //length of rectangle              int              breadth              ;              //breadth of rectangle              /* declaring member functions */              void              setLength              (              int              l              )              {              length              =              l              ;              }              void              setBreadth              (              int              b              )              {              breadth              =              b              ;              }              int              getArea              ()              {              return              length              *              breadth              ;              }              };              int              main              ()              {              Rectangle              rt              ;              rt              .              setLength              (              7              );              rt              .              setBreadth              (              4              );              int              area              =              rt              .              getArea              ();              cout              <<              "Area : "              <<              area              <<              endl              ;              return              0              ;              }            

Output

This was the same as the previous example but this time we defined the function at the time we declared it in the Rectangle class.

Access Modifiers


The access modifiers decide how the members of a class can be accessed. There are three types of access modifiers in C++.

  • public
  • private
  • protected

Public


When we declare any class member as public, that variable becomes available everywhere in the program, even outside the function in which it was declared. Let's see an example to understand this.

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              public              :              int              length              ;              //length of rectangle              int              breadth              ;              //breadth of rectangle              int              getArea              ()              {              return              length              *              breadth              ;              }              };              int              main              ()              {              Rectangle              rt              ;              rt              .              length              =              7              ;              rt              .              breadth              =              4              ;              int              area              =              rt              .              getArea              ();              cout              <<              "Area : "              <<              area              <<              endl              ;              return              0              ;              }            

Output

Since we declared the variables length and breadth as public, we directly accessed these and assigned them values.

rt.length = 7; - We directly accessed the data member 'length' by the object of the Rectangle class. Similarly, we assigned breadth to the Rectangle object by directly accessing it.
rt.getArea(); - We also accessed getArea() directly since it is also declared as public.

Thus we can access the members declared as public from anywhere.

Private


The member declared as private can only be accessed inside the class in which it is declared. Thus, the object of the class cannot directly access its members as we did in the case of public.

By default, all the members of a class are private.

If we try to access a private member of any class from outside that class, we will get a compile time error. Let's see an example.

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              int              length              ;              public              :              int              breadth              ;              void              setLength              (              int              l              );              int              getArea              ();              };              void              Rectangle              ::              setLength              (              int              l              )              {              length              =              l              ;              }              int              Rectangle              ::              getArea              ()              {              return              length              *              breadth              ;              }              int              main              ()              {              Rectangle              rt              ;              rt              .              setLength              (              7              );              rt              .              breadth              =              4              ;              int              area              =              rt              .              getArea              ();              cout              <<              "Area : "              <<              area              <<              endl              ;              return              0              ;              }            

Output

In this example, breadth and the functions setLength and getArea are declared public. The data member length is private since all the members are private by default.

Since we cannot directly access any private member, therefore cannot access length directly. So, we declared another member function 'setLength' as public which assigned a value 7 to the length.

The rest of the members were directly accessed from the main function.

Though the data member 'length' is by default private, we can declare it private as well as shown below.

class Rectangle
{
private:         int length;
public:
int breadth;
void setLength( int l );
int getArea();
};

Protected


Protected is similar to private. Any member declared as protected cannot be accessed outside the class but can be accessed by any subclass of that class.

We will learn more about subclass in a later chapter.

Constructor


A constructor is a special member function of a class which is called automatically when an object of that class is called. It has the same name as that of the class and has no return type.

Constructor is a special type of function which is used to initialize an object. It is invoked at the time of object creation.

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              public              :              int              length              ;              int              breadth              ;              Rectangle              ()              {              length              =              10              ;              breadth              =              10              ;              }              };              int              main              ()              {              Rectangle              rt              ;              cout              <<              "length = "              <<              rt              .              length              <<              endl              ;              cout              <<              "breadth = "              <<              rt              .              breadth              <<              endl              ;              return              0              ;              }            

Output

In this example when we created the object rt of class Rectangle, the constructor Rectangle() automatically got called and initialized the data members for the object 'rt'. It initialized the length and breadth of rt to 10 each.

When the constructor was called, length and breadth were created and then in the body of the constructor, these member variables were assigned values.

We can also make a constructor with nothing in its body.
Rectangle(){ };

Constructor having Parameters


We can also have constructors with parameters.

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              int              length              ;              int              breadth              ;              public              :              Rectangle              (              int              l              ,              int              b              )              {              length              =              l              ;              breadth              =              b              ;              }              int              getArea              ()              {              return              length              *              breadth              ;              }              };              int              main              ()              {              Rectangle              rt              (              7              ,              4              );              cout              <<              "Area : "              <<              rt              .              getArea              ()              <<              endl              ;              return              0              ;              }            

Output

In this example, we have parameters in our constructor. As told earlier, a constructor is also a function which is executed at the time of creating an object and has the same name as that of its parent class. So, it will work like a function and assign values passed from Rectangle rt( 7, 4 ); to length and breadth.

Rectangle rt( 7, 4 );

It will create an object 'rt' of class 'Rectangle' and pass 7 to 'l' and 4 to 'b' ( 'l' and 'b' are used in the constructor of the class 'Rectangle' ).

object and class in C++

Use of static


'static' is used to make access to any data variable or function without making an object of that class. It means that 'static' is used so that we can access any data variable or function without making an object of that class.

Let's see this:

                            #include              <iostream>                            using              namespace              std              ;              class              Rectangle              {              public              :              static              void              printArea              (              int              l              ,              int              b              )              {              cout              <<              l              *              b              <<              endl              ;              }              };              int              main              ()              {              Rectangle              ::              printArea              (              4              ,              7              );              return              0              ;              }            

Output

Our function 'printArea' is static. So, we directly used it on 'Rectangle' class, without making any object of it.

Note that for calling a member function by the class itself, we have to use :: in place of the Dot (.).

In this example, we made a member function static. Now let's see another example in which a data member is made static.

                            #include              <iostream>                            using              namespace              std              ;              class              A              {              public              :              static              int              a              ;              };              int              A              ::              a              =              8              ;              int              main              ()              {              cout              <<              A              ::              a              <<              endl              ;              return              0              ;              }            

Output

Here, the variable a is made static and thus we directly accessed it in the main function.

Returning and passing object in a function


Yes, we can return or pass object(s) to a function. Let's see an example.

                            #include              <iostream>                            class              Account              {              public              :              int              balance              ;              public              :              Account              (){              balance              =              0              ;              }              public              :              static              Account              getAcc              (              Account              a              ,              Account              b              ){              Account              ac              ;              ac              .              balance              =              a              .              balance              +              b              .              balance              ;              return              ac              ;              }              };              int              main              (){              using              namespace              std              ;              Account              a1              ;              a1              .              balance              =              50              ;              Account              a2              ;              a2              .              balance              =              60              ;              Account              b              =              Account              ::              getAcc              (              a1              ,              a2              );              cout              <<              b              .              balance              <<              endl              ;              }            

Output

As you have seen, our method 'getAcc' is creating an 'Account' object by taking two 'Account' objects and returning it also.

b = Account.getAcc(); - 'getAcc' will create and return a new 'Account' object and 'b' will become equal to that object.

Here, we have directly used 'getAcc' method on class 'Account' ( Account.getAcc() ) because 'getAcc' is a static method ( static Account getAcc(Account a, Account b){ ).

If there is no constructor in a class, compiler automatically creates a default public constructor.

Even if we don't declare any constructor for a class, the compiler automatically declares a constructor which gets called when any object of that class is created and initializes its data members.

Suppose we made a class Vehicle without any constructor, then automatically its constructor will be created with nothing in its body as follows.

class Vehicle{
public:         Vehicle(){ }
}

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Talent is good, Practice is better, and Passion is Best
-Frank Lloyd Wright

how to create a class in c++

Source: https://www.codesdope.com/cpp-classes-and-objects/

Posted by: riverahiscriand68.blogspot.com

0 Response to "how to create a class in c++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel