• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Functions in C++

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 functionsstructure is a collection of variables
Contents is private, protected or publicContents is public / global
It provide security for dataIt does not provide security for data
It support polymorphism and inheritanceIt 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 classNon membersMember of derived classMember of Other class
PrivateYesNoNoNo
ProtectedYesNoYesNo
PublicYesYesYesYes

 

 

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.

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Functions in C++"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.