Sie sind auf Seite 1von 26

Functions in C++

Specifying a Class and Member


Function Specification

1
Object Oriented Programming By Amruta Vidwat
Overview
Functions in C++
Specifying a class
Declaring objects
Accessing class members
Member function specification
Making outside member function inline

Certified Course in Object Oriented Programming 2


The main() Function
In C++, return type of main() function is int by default.
(It should use return statement for termination)
In C, main() does not return any value.

Certified Course in Object Oriented Programming 3


Function Prototype
It describes details such as the number and type of arguments
and type of return values.

Syntax:
Type function-name (argument list);

Ex. float sum (int a, float b);


float sum (int, float) // Acceptable

Certified Course in Object Oriented Programming 4


Passing Arguments
Three methods to pass values to function:
1. Call by value (Pass by value)
2. Call by address (Pass by address)
3. Call by reference (Pass by reference)

C++ supports all three.


C supports first two.

Certified Course in Object Oriented Programming 5


Pass by Value
#include <iostream>
void change (int);
int main()
{
int num = 5;
cout << "In main(), value of num is " << num << endl;
change(num);
cout << "In main(), value of num is now " << num << endl;
return 0;
}
void change(int num)
{
cout << "In change(), value of num is " << num << endl;
num += 10;
cout << "In change(), value of num is now " << num << endl;
}

Certified Course in Object Oriented Programming 6


Result
In main(), value of num is 5
In change(), value of num is 5
In change(), value of num is now 15
In main(), value of num is now 5

Num in main and num in change: they have different addresses, which means that
they are not the same variable.
Num in main is unaffected.

Certified Course in Object Oriented Programming 7


Pass by Address
#include<iostream>
using namespace std;
void change(int *p);
int main()
{
int num=5;
cout<<"in main(), value of num is:"<<num<<endl;
cout<<"in main(), address of num is:"<<&num<<endl;
change(&num);
cout<<"in main(), value of num is now:"<<num<<endl;
return 0;
}
void change(int *p)
{
cout<<"in change, value of p is:"<<p<<endl;
cout<<"in change, address of p is:"<<&p<<endl;
cout<<"in change, value of variable pointed by p is:"<<*p<<endl;
*p+=10;
cout<<"in change, value of variable pointed by p is now:"<<*p<<endl;
}

Certified Course in Object Oriented Programming 8


Result

Certified Course in Object Oriented Programming 9


Pass by Reference
#include<iostream>
using namespace std;
void change(int &);
int main()
{
int num=5;
cout<<"in main(), value of num is:"<<num<<endl;
cout<<"in main(), address of num is:"<<&num<<endl;
change(num);
cout<<"in main(), value of num is now:"<<num<<endl;
return 0;
}
void change(int &p)
{
cout<<"in change, value of p is:"<<p<<endl;
cout<<"in change, address of p is:"<<&p<<endl;
p+=10;
cout<<"in change, value of p is now:"<<p<<endl;
}

Certified Course in Object Oriented Programming 10


Result

Certified Course in Object Oriented Programming 11


Specifying a Class
Class contains data and functions to operate on that data.
Class declaration specifies its data members, member
functions and their scope.
General syntax of class declaration is:

class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Certified Course in Object Oriented Programming 12
Access to members
Private members: can be accessed only from within the class.
Protected members: can be accessed by own class and its
derived classes.
Public members: can be accessed from outside the class also.

Certified Course in Object Oriented Programming 13


Declaring Object
Object is an instance of class.
Process of defining an object is called instantiation.

book b1;

Characteristics:
It is individual.
It points to physical or logical thing.
Its scope is limited to the block in which it is defined.

Certified Course in Object Oriented Programming 14


Accessing Class Members
Syntax:
object_name . Fuction_name (argument list)

Class member access operator

Class members can be accessed only through the objects of


that class.
Private data members can not be accessed in the main (), they
are accessible only inside the class.

Certified Course in Object Oriented Programming 15


Member Function Specification
Member function should be declared inside the class.
They can be defined a) Inside the class
b) Outside the class
Member functions defined inside the class are treated as Inline
Function

Certified Course in Object Oriented Programming 16


Member Function Inside The Class
It can be declared in public or private section.
When function is declared as inline, it increases the program
execution speed.
It is useful when function is small.
Due to the use of inline function, program needs more
memory.

Certified Course in Object Oriented Programming 17


Example: Member Function Inside The Class
# include <iostream.h>
# include <contream.h>
class book
{
private:
int qty;
float price;
public:
void output_val()
{
qty=10;
price=125;
cout<<\n qty= <<qty;
cout<<\n price= <<price;
}
};

int main()
{
book one; // Object declaration
one.output_val(); // Call to public member function
return 0;
}

Certified Course in Object Oriented Programming 18


Private Member Function
# include <iostream.h> int main()
# include <contream.h> {
class book book one;
{
one.use();
private:
int qty; one.output_val();
float price; return 0;
void use() }
{
qty=10;
price=125;
}
public:
void output_val()
{
use (); // Call to private member function
cout<<\n qty= <<qty;
cout<<\n price= <<price;
}
};
Certified Course in Object Oriented Programming 19
Practice Programs on class and Object
1. Program to Enter Students Details ( Name, Division, Roll No. Year) and Display it.
(member function inside the class).
2. Write a program to display one integer value and one float value using two separate
functions. Use pass by value for integer function. (member functions inside the
class).

Certified Course in Object Oriented Programming 20


1. Solution
#include <iostream>
int main()
using namespace std;
{
class stud stud s;
{ s.enter();
public: s.display();
char name[30],clas[10];
int rol,age;
}
void enter()
{
cout<<"Enter Student Name: "; cin>>name;
cout<<"Enter Student Age: "; cin>>age;
cout<<"Enter Student Roll number: "; cin>>rol;
cout<<"Enter Student Class: "; cin>>clas;
}

void display()
{
cout<<"\n Age\tName\tR.No.\tClass";
cout<<"\n"<<age<<"\t"<<name<<"\t"<<rol<<"\t"<<clas;
}
};

Certified Course in Object Oriented Programming 21


2. Solution
#include <iostream> int main()
using namespace std; {
Test o1, o2;
class Test float Object2;
{ int d;
private: cout<<"Enter Integer";
int data1; cin>>d;
float data2; o1.IntegerData(d);
Object2 = o2.FloatData();
public:
cout << "You entered " << Object2;
void IntegerData(int d)
return 0;
{
}
data1 = d;
cout << "Number: " << data1;
}

float FloatData()
{
cout << "\nEnter data: ";
cin >> data2;
return data2;
}
};

Certified Course in Object Oriented Programming 22


Member Function Outside The Class
# include <iostream.h>
# include <contream.h>
class book
{
private:
int qty;
float price;
public:
void output_val() // Declaration
};

void book :: output_val() // Definition outside the class


{
qty=10;
price=125;
cout<<\n qty= <<qty;
cout<<\n price= <<price;
}

int main()
{
book one; // Object declaration
one.output_val();
return 0;
}

Certified Course in Object Oriented Programming 23


Making an Outside Member Function Inline
# include <iostream.h>
# include <contream.h>
class book
{
private:
int qty;
float price;
public:
void output_val() // Declaration
};

inline void book :: output_val() // Outside inline function


{
qty=10;
price=125;
cout<<\n qty= <<qty;
cout<<\n price= <<price;
}

int main()
{
book one; // Object declaration
one.output_val();
return 0;
}

Certified Course in Object Oriented Programming 24


Practice Programs of classes and objects
1. Program to Enter Students Details and Display it (member function outside the
class)

Certified Course in Object Oriented Programming 25


Thank You..

Certified Course in Object Oriented Programming 26

Das könnte Ihnen auch gefallen