You are on page 1of 9

OOP Lecture - 2

Arjay V. Ordonio
Functions

A function is defined as a group of statements or a block of code that carries out


specific tasks.
Functions bring out modularity in our programs.
Function and Data member

The function declared within the class is usually called as


member function in C++ and it is called method in object
oriented programming.
The data declared along with some data type is called as the
member data of that class.
The date members denote the property of the class and the
member functions denote the operations on that data.
Normally in C++ the data within the class is declared as
private and the member functions are declare as public.
Program that demonstrate the class, access specifiers and dat member function and
data members.
#include <iostream.h>
Class Test
{
private: Data members are
int a, b, c; declared as private

public:
int Addition(int a, int b)
{ Members function are declared as
c = a+b; public.
Note that: Only within the function
return c;
the data members can be
}j manipulated
void Dislay()
{cout << “The sum is: ” << c<<“\n;}
};
Void main()
{
Test obj;
Only member functions are
obj.Addition(10,20); accessible outside outsid the
obj.Display(); class because they are public.
}

Output
The sum is: 30
Write a C++ program to check whether an integer is a Prime or a composite number:
#include <iostream>
using namespace std;

class Test
{
int num;
public:

void Get_num(){
cout<<"Please Enter a positive integer"<<endl;
cin>>num;
}

void Check_prime(){
int flag = 1;
for(int n = 2; n<=num -1; n++){
if(num % n==0){
flag=0;
}
}
if(flag == 1){
cout<<num<<"is a prime number"<<endl;
}
else
cout<<num<<"is a composite number"<<endl;
}
};

void main()
{
Test obj;
obj.Get_num();
obj.Check_prime();

system("pause");
}
Default Arguments

Default Argument is an argument to the function to which


the default value is provided. If the user does not supply a
value to this argument then the default value is used. But if
the user provides some different value to this argument then
this new value is assigned to the default parameter.
Example:

#include <iostream>
using namespace std;

class Test
{
private:
int x, y;

public:
void fun(int x, int y=100){
cout << "x= "<< x <<endl;
cout << "y= "<< y <<endl;
}

};

void main()
{
Test obj;
cout << "Displaying the default argument value" <<
endl;
obj.fun(10);
cout << "Overriding the default argument" << endl;
obj.fun(10,20);

system("pause");
}
Function Overloading

The Function Overloading is a concept in which one


can use many functions having the same function
name but can pass different number of parameters
or different types of parameter.

Same name, different arguments


Example: #include <iostream>
using namespace std;

class test{
public:
int sum(int, int);
float sum(float, float);
double sum(double, double);
};

int test::sum(int a,int b){


return (a+b);
}
float test::sum(float a,float b){
return (a+b);
}
double test::sum(double a, double b){
return (a+b);
}

void main(){
test obj;
int choice;
int a,b;
float x,y;
double m,n;
double result = 0;
cout<<"\n\t\t Main Menu";
cout<<"\n\t1. Addition of two integer numbers.";
cout<<"\n\t2. Addition of two float numbers.";
cout<<"\n\t3. Addition of two double numbers."<<endl;
cout<<"\nEnter your choice: ";
cin >> choice;
switch(choice){
case 1: cout <<"\nEnter 2 numbers: ";{
cin>>a>>b;
result = obj.sum(a,b);
break;}
case 2: cout <<"\nEnter 2 numbers: ";
cin>>x>>y;
result = obj.sum(x,y);
break;
case 3: cout <<"\nEnter 2 numbers: ";
cin>>m>>n;
result = obj.sum(m,n);
break;
default:cout <<"\nWrong choice:";
break;
}
cout << "\n\nResult: " << result <<endl;

system("pause");
}

You might also like