FUNCTIONS
• Function is the building block of C++.
• Used to reduce the size of the program by
calling and using them in different places in
the program.
• main()Function returns an integer value.
int main();
int main(int arg);
 Function return a value using return statement.
FUNCTION PROTOTYPE
• The function interface to the compiler by
giving the details like number, data type,
arguments – function prototype.
• It is a declaration statement in calling program
type function name (argument list);
Eg:
float volume (int x, float y, z);
float volume (int, float, float);
CALL BY REFERENCE
• C++ permits to pass the parameters to the
functions by reference variable.
• When it is passed the formal argument act as
the actual argument in the calling function
void swap(int a, int b)
{
int t = a;
a = b;
b = t;
}
INLINE & RECURSION FUNCTION
• An inline function is a function that is expanded in
line when it is invoked.
• The compiler replaces the function call with the
corresponding function code.
inline function header
{
function body
}
• It send a request not a command
• Recursion means function call by itself ie one of the
statement in the function definition makes a call to
the same in which it is present.
CLASS
• Collection of objects – CLASS
• The entire set of data and code of an object can
be made a user defined data type by using
class.
• 2 Parts:
– Class Declaration – describe the type & scope of
its members
– Class function definitions – how the class
function are implemented.
• General format:
class class-name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void);
};
• Class member – the class body contains the
declaration of variables and functions.
• Data member – variables declared inside the
class.
• Member function – the function declared
inside the class.
DEFINING MEMBER FUNCTIONS
• Done by 2 ways
1. Outside the class definition
2. Inside the class definition
Outside the class definition
• Member function declared inside the class has to be
defined separately outside the class.
return-type class-name :: function-name
(argument declaration)
{
function body;
}
void item :: getdata (int a , float b)
{
number = a;
cost = b;
}
void item :: putdata (void)
{
cout<< “Number : “ << number;
cout << “Cost : “ << cost;
}
Inside the class definition
• It is to replace the function declaration by the actual
function definition inside the class.
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata(void)
{
cout << number;
cout << cost;
}
};
Functions, classes & objects in c++

Functions, classes & objects in c++

  • 2.
    FUNCTIONS • Function isthe building block of C++. • Used to reduce the size of the program by calling and using them in different places in the program. • main()Function returns an integer value. int main(); int main(int arg);  Function return a value using return statement.
  • 3.
    FUNCTION PROTOTYPE • Thefunction interface to the compiler by giving the details like number, data type, arguments – function prototype. • It is a declaration statement in calling program type function name (argument list); Eg: float volume (int x, float y, z); float volume (int, float, float);
  • 4.
    CALL BY REFERENCE •C++ permits to pass the parameters to the functions by reference variable. • When it is passed the formal argument act as the actual argument in the calling function void swap(int a, int b) { int t = a; a = b; b = t; }
  • 5.
    INLINE & RECURSIONFUNCTION • An inline function is a function that is expanded in line when it is invoked. • The compiler replaces the function call with the corresponding function code. inline function header { function body } • It send a request not a command • Recursion means function call by itself ie one of the statement in the function definition makes a call to the same in which it is present.
  • 6.
    CLASS • Collection ofobjects – CLASS • The entire set of data and code of an object can be made a user defined data type by using class. • 2 Parts: – Class Declaration – describe the type & scope of its members – Class function definitions – how the class function are implemented.
  • 7.
    • General format: classclass-name { private: variable declaration; function declaration; public: variable declaration; function declaration; }; Class item { int number; float cost; public: void getdata(int a, float b); void putdata(void); };
  • 8.
    • Class member– the class body contains the declaration of variables and functions. • Data member – variables declared inside the class. • Member function – the function declared inside the class.
  • 9.
    DEFINING MEMBER FUNCTIONS •Done by 2 ways 1. Outside the class definition 2. Inside the class definition
  • 10.
    Outside the classdefinition • Member function declared inside the class has to be defined separately outside the class. return-type class-name :: function-name (argument declaration) { function body; }
  • 11.
    void item ::getdata (int a , float b) { number = a; cost = b; } void item :: putdata (void) { cout<< “Number : “ << number; cout << “Cost : “ << cost; }
  • 12.
    Inside the classdefinition • It is to replace the function declaration by the actual function definition inside the class. class item { int number; float cost; public: void getdata(int a, float b); void putdata(void) { cout << number; cout << cost; } };