Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, June 15, 2021

C++ Classes and Objects

Class: In C++, A class is a user defined data type declared with class keyword. All the variables and the functions are bind inside a class.

C++ Classes & Objects | welcome2protec
C++ Classes & Objects | welcome2protec

Classes in C++

C++ is an object oriented programming languages. Everything in C++ is associated with classes and object along with its attributes (variables) and functions (methods). Note: Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members"

Class: A class is a user defined data type or data structure declared with class keyword. All the variables and the functions (methods) are bind inside a class using by three access specifiers private, protected and public. By default, access to members of a C++ class is private.

Simply you can say: A class is like a blueprint of data members (Variables) and functions (Methods), it helps us to group data members and methods using available access specifiers.

Note: When a class is defined, only the specification for the object is defined; no memory or storage is allocated until the object is created. To use the data and access functions defined in the class, we need to create objects.


Creating a Class in C++

A class is defined using class keyword followed by the name of the class. The body of the class is defined inside the curly braces. Let say we have a class named Car which has data members (variables) name, speed, weight and functions such as break() and SlowDown(). Have a look at the below example:

/*Creating a class with name Car*/
class Car
{
  //Data members
  char name[25];
  int speed;
  int weight;
  
  public:
  //functions
  void break(){
  ---
  }
  void SlowDown(){
  ---
  }
};

int main{
 //here bmw is an object of car class
 Car bmw;
}

Example Explanation:

  • The class keyword is used to create a class called Car
  • Inside the class, data members (variables) name, speed, weight and functions such as break() and SlowDown(). When variables and methods are declared within a class, they are called data members.
  • The public keyword is an access specifier, which specifies that members (variables and functions) of the class are accessible from outside the class. You will learn more about access specifiers later.
  • Three access specifiers private, protected and public. By default, access to members of a C++ class is private. The private members are not accessible outside the class, they can be accessed only through methods of the class. Only the public members of the class are accessible outside the class.
  • At last, end the class definition with a semicolon ;

Object In C++:

In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an entity that has state and behavior. Here, state means data (like variables) and behavior means functionality (functions).

In real life a car is an object. The car has attributes, such as weight, color, price speed, and methods or functions like drive(), gearChange(), slowDown(), and brake() etc. Object is a runtime entity, as it is created at runtime. Let's have a look at the below diagram to uderstand it better.


Classes & Objects | welcome2protec
Classes & Objects | welcome2protec

Creating an Object:

To create an object of a class person, specify the class name, followed by the object name. For Example:

/* Here 'p' is an object of a 'person' class*/
 person p;
}

In this example, person is the type and p is an object (also known as reference variable) that refers to the instance of of 'person' class.


Object is an instance of a class which help us to get access the particular variables and functions using dot (.) operator with the object name. For example: if the name of object is p of a person class and you want to access the member variable with the name name then you will have to write p. name. Let's understand with the below example:

/*Creating a class with name person*/
#include <stdio.h>
using namespace std;

class person
{
  //Data members
  public:
   string name;
   int age;
   int weight;
};

void main{
 //here p is an object or reference of 'person' type
 person p;
 p.name="Nafish"
 p.age=27;
 p.weight=60;
}

To access, use, and initialize the private data member you need to create getter and setter functions, to get and set the value of the data member. The setter function will set the value passed as argument to the private data member, and the getter function will return the value of the private data member to be used.








Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us