Vous êtes sur la page 1sur 3

#include <conio.

h>
#include <iostream>
using namespace std;
int Add(int, int, int, int);
int Multiply(int, int, int, int);
int main()
{
int a,b,c,d;
cout << "Enter 1st No: "; cin >> a;
cout << "Enter 2nd No: "; cin >> b;
cout << "Enter 3rd No: "; cin >> c;
cout << "Enter 4th No: "; cin >> d;
cout << "\n\n The Addition of No's is : " << Add(a,b,c,d);
cout << "\n\n\n The Multiplication of NO's is : " << Multiply(a, b, c, d);
getch();
return 0;
}
int Add(int a, int b, int c, int d)
{
return a + b + c + d;
}
int Multiply(int a, int b, int c, int d)
{
return a*b*c*d;
}
**************************************************************************
a:
the function
int sum(num1,num2)
{ return num1+num2; }
can be called by two ways which are as
void main()
{
int a,b,c;
a=sum(b,c); //functions return value is assigned to a variable
}
other method is
void main()
{
int a,b ;
cout<<sum(a,b); // functions return value is send to output directly

}
c
b:
The default arguments are given at function definition time. The idea behind def
ault argument is very simple. If a function is called by passing argument/s, tho
se arguments are used by the function. But if all argument/s are not passed whil
e invoking a function then, the default value passed to arguments are used. Defa
ult value/s are passed to argument/s in function prototype
which can be explained as
#include<iostrem>
#include<conio.h>
using namespase std;
int sum(int =2, int=3);
void main()
{
sum();

// both default arguments will be passed

sum(4);

// 1 argument given and 1 default passed

sum(4,5);

//both arguments given and no default passed

}
********************************************************************************
They are both two totally different concepts. Encapsulation is about hiding impl
ementation details whereas Inheritance is about re-using code.
Encapsulation
Encapsulation allows the programmer to group data and the subroutines that opera
te on them together in one place, and to hide irrelevant details from the user.
The basic idea of encapsulation is information hiding which is making objects an
d algorithms invisible to portions of the system that do not need them.
It advantage is that the object need not reveal all of its attributes and behavi
ors
For Example
The power steering mechanism of a car. Power steering of a car is a complex syst
em, which internally have lots of components tightly coupled together, they work
synchronously to turn the car in the desired direction. It even controls the po
wer delivered by the engine to the steering wheel. But to the external world the
re is only one interface is available and rest of the complexity is hidden
This information hiding can be implementd by public, private and protected acces
s specifiers of class in C++
Inheritance
One of the most important concepts in object-oriented programming is that of inh
eritance. Inheritance allows us to define a class in terms of another class, whi
ch makes it easier to create and maintain an application. This also provides an
opportunity to reuse the code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member
functions, the programmer can designate that the new class should inherit the m
embers of an existing class. This existing class is called the base class, and t
he new class is referred to as the derived class.
The idea of inheritance implements the is a relationship. For example, mammal IS
-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
It can be expained as
Class Base
{
}
Class Drived: public Base
{
}

Vous aimerez peut-être aussi