mobileNavlogo
headerlogo

Blog

Functions in C++

21 September, 2018

Related Blogs

Functions

  • Inline functions
  • Function default arguments
  • Function overloading
  • Pass by value
 

Classes and Objects  in C++

Class Class is a data type or user defined data type. It is a collection of members, these members are 2 types. Data members Member functions In object oriented application development data is represent in the form of object  

Difference between class & structure

 
class in c++  Structure in ‘C’
It is a collection of variables and functions structure is a collection of variables
Contents is private, protected or public Contents is public / global
It provide security for data It does not provide security for data
It support polymorphism and inheritance It does not support polymorphism and inheritance
    Syntax of class in C++ class  class-type-name { private : data-members; member-functions; public : data-members; member functions; }  

What is data member ?

Variable declared inside class is called data member Data member define state of the object or value hold by object.  

Member function in C++

Function defined inside a class is called member function Binding data members with member function inside a class called as Member function define behavior of object or operation performed on the object Private, public and protected are called as access specifier. Default members of class are private Default members of structure are global/ public  

Access specifier/ access neighbours

Access specifier define visibility on accessibility of members declared within class. C++ provide 3 access specifiers
  1. private
  2. protected
  3. public
 

Private

Default members of class are private Private member are accessible within class or members of the same class Private members are not accessible within non members on members of other classes.   Screenshot_14     Data hiding in object oriented is achieved by declaring data members within class as private Protected Protected member access by members of same class and derived class Protected members cannot access by non members and members of other classes.     Screenshot_15       public These  members access by members of some class, derived class, non members and members of other classes.   Screenshot_16    

Summary of Access Specifiers

 
  with in class Non members Member of derived class Member of Other class
Private Yes No No No
Protected Yes No Yes No
Public Yes Yes Yes Yes
   

Object in C++

  • It is a instance of class
  • A variable of type class is called object
  • On creation of object memory is allocated for data members of class.
Syntax Class –name object –name, object-name,…. class defined structure of object  

Program (Write a program to find area of triangular)

# include <iostream.h>                   [ writing a class within main- local class triangle                                             classes & vice versa] { private : float base, height;                 // data members public : void read() { cout<<”\n input base height”; cin>>base>>height; } void find area () { float area=0.5*base*height; }; Screenshot_17 Size of the class is equal to some of data member size On creation of object memory is allocated for data members of class but not member functions Member functions operators on one or more than one object.  
About Author
Authorlogo
Name

TekSlate

Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .