Concept of Classes and Objects in C++
×

Concept of Classes & Objects in C++

3194

We all are familiar with object-oriented pillars of C++ in our previous topics such as objects, classes, encapsulation, and abstraction. Now it's time to put all those concepts into practice. Let us learn how to represent complex number class in C++ programming language. As we all know the concept of complex number from our 10th class.

The class is the keyword with the class name. The class contains some data members and memory functions which were known as attributes and behaviors respectively. While declaring member function there required access specifiers and in C++ there are three access specifiers are present i.e. public, private and protected.

The syntax of Class Definition is given below:

Class class_name
{
any Data Members;
any Methods;
};

Data member and member function are the advanced features that are introduced in C++. The class comprises three thing Access Specifiers or modifiers, Data members and methods. Now let us discuss what Data members are and what its role in class is.

Data members
The main objective of the class to hold the information. This objective is achieved with the help of attributes and that attributes are known as Data members.

Data members are the object's property. We can define a variable as an object as any variables that are defined inside the class with the use of basic data types such as structure, pointers, class etc. are known as Data members.  The declaration of the data members is identical to the declaration of a variable. They can be private or public but mostly held private. The example Below illustrates the use in both private and public.

class M {
private:
int n;
public:
float s;
};

Static Data members
These members are declared with the use of the Static keyword and its usage is always applicable in the static member functions.

The syntax of static data members is given below:

class Bottle
{
static int color;
.....
};

Data members can be accessed as public, private and protected.

Access Specifiers
The public data members can be accessed in the similar method however the data members under private are not allowed to be directly accessed by the object. It depends upon the access control of the specific data member and In C++ that access control can be given through Access specifiers.

With the help of access specifiers, members of the class are called. Through it determines the security and visibility of the member functions. There are three types of access specifiers i.e. public, private and protected.

* Public - The data members declared as Public are accessible from outside the Class through an object of the specified class.
* Protected - They are also accessible from outside the class but through derived class.
* Private - These data members are private i.e. only accessible within the class.

A simple C++ example illustrating the public, private and protected.

class MyClass
{
public:
int p;
protected:
int q;
private:
int r;
};
int main()
{
MyClass obj;
obj.p = 25; // allowed
obj.q = 6; // gives compiler error
obj.r = 26; //gives compiler error
}

Member function
Functions that are declared inside the class body are known as a member function.

Member functions that can be defined inside the objects are generally useful for data member's modification and behavior determination of the class.

Member functions, there are three type of member functions found in c++ i.e. Constructor, destructor and normal member function.

Normal member function

Example

void display()
{
cout<<mreal<<"+"
class complex m
{
private:
int mreal; // datamember
int mimg; // datamember
};

Member functions can be defined in two ways:

  * Inside class definition
  * Outside class definition

In these, both methods, body of the function remain identical except the function header.

A simple program defining member function both inside and outside class

#include<iostream>
using namespace std;
class website
{
public:
string name;
int rank;
void displayrank();
void displayname()

{
cout << "Our website name is: " << name;
}

};

void website :: displayrank()

{
cout << "Our website rank is: " << rank;
}

int main()

{
website obj;
obj.name ="codingtag";
obj.rank = 2;
obj.displayrank();
cout<<endl;
obj.displayname();
return 0;
}

Output:
Our website rank is: 2
Our website name is: codingtag


Let us understand class and object concepts with the help of a simple program.

Through the class, data members and member functions are declared and they are executed with the help of an object. Members of the class are called through objects. For e.g. in the following program, website name is called through obj and input is integrated as "cogingtag" and with the same object member function display() is also called.

#include<iostream>
using namespace std;
class website
{
public: // access specifier
string name; // data member
void display() // memberfunction
{
cout << "Our website name is: " << name;
}
};
int main()
{
website obj;
obj.name ="codingtag";
obj.display();
return 0;
}

Output:
Our website name is: codingtag



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments