C++ Classes & Objects

The main goal of c++ is they are provide object oriented programming feature with using of classes and object. So it is also called as "C with classes".

Syntax of class

class example
{
  //Access specifiers
  //Data member
  //Member function
};

Definition of class start with 'class' keyword followed by name of class. curly braces are used to define data member, member function and access specifier. and end with semicolon. Class is a combination of data member and member function. access specifiers used to provide abstraction and access control of class member function.In this post we will learn about all basic and special terminology of c++ class.

Object

object is instance of a class. help of object we can access member function of class. object are create using following syntax.

 class_name object_name;

class name and followed by object name. For example

//example : class name
//obj1 : object name  
example obj1;

Remember that object name and help of dot operator we are access the member function of class

Access specifiers

Cpp is provide three level abstraction and access control of data member and member function. this is also known as access specifiers of c++ programming. there as follows public private and protected. By default access specifiers of c++ is private.

public

We can use public data member using of same class and inherit subclass.

class notes{ 
  public:
    int x,y; //data member
    void message(); //member function
};
private

Private function and variable only accessible within the constructor and member function of class.

class Book{ 
  private:
    //data member
    int page; 
    folat price;
    //member function
    void book_info();
    void detail(); 
};
protected

Protected member function are accessible by within the class function and inherited class.

class student{ 
  protected:
    int age,roll_no; //data member
    void attendance(); //member function
};

Data member

Defined data inside a class that is called Data member. they are predefined and user defined data type. or we can say variable of class is data member..

Member function

Member function is class functions and methods. that are associated with different operations and relative task. we are used those data member using class object. we can define member function inside of any specifiers (like public private protected) see the example to define member function of class.

//created class employee
class employee{
  public: //Access specifiers
    void bonus(int); //member function
  protected://Access specifiers
    void salary(int); //member function
  private: //Access specifiers
    void balance();  //member function
};

In class employee we are declaring three function in three different access specifiers. public member function are directly accessible by class employee object and inherited class. but protected and private member function are more restricted. they are not directly accessible by class object. this can only used inside member function. see example.

#include<iostream>
using namespace std;

class employee{
  public: //Access specifiers
    float total_pay;
    //member function
    void bonus(int balance){
       cout<<"Initial Amount :"<<balance<<endl;
       cout<<"100000 is bonus of new years."<<endl;
       salary(balance);//call protected function
    }
  protected://Access specifiers
    void salary(int amount){
       //logic here
      total_pay=10000+amount;
        balance();//call private function 
    } 
  private: //Access specifiers
    void balance(){
       cout<<" Total Amount :"<<total_pay<<endl;
    }
};


int main(){
  //create object e1 of class employee
  cout<<"Result of Obj1"<<endl;
  employee obj1;
  obj1.bonus(500000);

    cout<<"\nResult of Obj2"<<endl;
  employee obj2;
  obj2.bonus(200000);

}
Output
Result of Obj1
Initial Amount :500000
100000 is bonus of new years.
 Total Amount :510000

Result of Obj2
Initial Amount :200000
100000 is bonus of new years.
 Total Amount :210000

Note that in this program we are call protected and private function inside a member function. This process is also known as nested of member function. And in this program defined all the function as inline. we can also defined function as outside of class.

Member function in outside class


  //syntax like this
  return_type class_name :: function_name(parameter_list){
    //function body
  }
 /*
  return_type: Data which are returned by function. 
              They can used defined and predefined data
  class_name :  Name of class
  function_name: Name of function
  parameter_list: Defined parameter  
  :: (scope resolution operator)
 */

see example to defined function outside of class

/*
Example to Defined function outside of class
*/
#include<iostream>
using namespace std;

class calculator{ 
  public:
  //Declared member function
  void addition(float,float);
  void multiply(int,int);
};
/*---------------
+ void : return type
+ calculator : class name
+ addition : function name
+ (float num1,float num2) : parameter list
+ Work: basic example to add given two float number
----------------*/
void calculator :: addition(float num1,float num2){
  //local variable of function
  float result=num1+num2;
  //display result
  cout<<"Addition ("<<num1<<" + "<<num2<<") :"<<result<<endl;
}

/*---------------
+ void : return type
+ calculator : class name
+ multiply : function name
+ (int num1,int num2) : parameter list
+ Work: basic example to multiply given two integer number
----------------*/
void calculator :: multiply(int num1,int num2){
//local variable of function
  int result=num1*num2;
  //display result
  cout<<"Multiply ("<<num1<<" *"<<num2<<") :"<<result<<endl;
}

int main(){
  //Create object operation of class calculator
  calculator operation;
  operation.addition(14.5,35);
  operation.multiply(15,4);
}
Output
Addition (14.5 + 35) :49.5
Multiply (15 *4) :60

© 2019, regularcodes.com, All rights reserved