Unit 1
Unit 1
i. Data encapsulation
ii. Data hiding and access mechanisms
iii. Automatic initialization and clear-up Of Objects
iv. Operator overloading
Introduction to C++
Simple Program
Program Explanation
The iostream File - We have used the following #include directive in the
program #include<iostream.h>. This directive is the preprocessor to add
the contents of the iostream.h file to the program. It contains declarations
for the identifier cout and the operator <<. The header file iostream should
be included at the beginning of all programs that use input/output
statements.
Variable – Variable are those whose value is not fix. The program uses
any number of variables. They can be declared as type of data type by the
statement. All variables must be declared before they are used in the
program. Example: float num;
#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void putdata(void);
};
void person::getdata(void)
{
cout<<”enter the name”;
cin>>name;
cout<<””enter age”;
cin>>age;
}
void person::putdata(void)
{
cout<<”name
is:”<<name;
cout<<”age is:”<<age;
}
void main()
{
person p;
clrscr();
p.getdata();
p.putdata();
getch();
}
As it can be seen from the above program, a typical C++ program would
contain four sections as shown in program. These sections may be placed in
separate code files and then compiled independently or jointly.
1. Objects 5. Inheritance
2. Classes 6. Polymorphism
3. Data abstraction 7. Dynamic binding
4. Data encapsulation 8. Message passing
Classes - We know that objects contain data, and code to manipulate that
data. The entire set of data and code of an object can be made a user-
defined data type with the help of a class. In fact, objects are variables of
the type class. Once a class has been defined, we can create any number of
objects belonging that Class. Each object is associated with the data of
type class with which they are created. A class is thus a collection of
objects of similar type. For example, mango, apple and orange are
members of the class fruit. Classes are user-defined data types and behave
like the built-in types of programming language. The syntax used to create
an object is no different than the syntax used to create an integer object in
C. If fruit has been defined as a class, then the statement fruit mango; will
create an object mango belonging to the class fruit.
Applications of OOPs
OOP has become one of the programming buzzwords today. There appears
to the great deal of excitement and interest among software engineers in
using OOP. Applications of OOP are beginning to gain importance in
many areas. The most popular application of object oriented
programming, up to now, has been in the area of user interface design
such as windows, Real-business system. The promising areas for
application of OOP include:
Inline function
Function - A function is a set of statements that takes input, does
some specific computation, and produces output. The idea is to put
some commonly or repeatedly done tasks together to make
a function so that instead of writing the same code again and again for
different inputs, we can call this function. In simple terms, a function is
a block of code that runs only when it is called.
Example
Types of Functions
C++ has a different solution to this problem. To eliminate the cost of calls
to small functions, C++ proposes a new feature called inline function.
An inline function is a function that is expanded in line when it is
invoked, That is, the compiler replaces the function call with the
corresponding function code (something similar to macros expansion).
The inline functions defined as follows:
inline function-header
{
function body
}
Example
inline double cube(double a)
{ return (a*a*a); }
Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 13
The above inline function can be invoked by statements like c = cube
(3.0);
This makes the inline feature far superior to macros. It is easy to make a
function inline. All we need to do is to prefix the keyword inline to the
function definition, All inline functions must be defined before they are
called. We should exercise care before making a function inline. The speed
benefits of inline functions diminish as the function grows in size. At
some point the overhead of the function call becomes small compared to
the execution of the function, and the benefits of inline functions may be
lost. In such cases the use of normal functions will be more meaningful.
Some of the situations where inline expansion may not work are:
1. For functions returning values. if a loop, a switch, or a goto exists.
2. For functions not returning values, if a return statement exists.
3. If functions contain static variables.
4. If inline function are recursive.
#include<iostream.h>
#include<conio.h>
int main( )
{
clrscr();
float a=2.5;
float b=3.5;
cout<<"Multiplication is :"<<mul(a, b);
return0;
}
1. The values passed in the default arguments are not constant. These
values can be overwritten if the value is passed to the function. If not,
the previously declared value retains.
Example
#include <iostream.h>
#include <conio.h>
void main()
{
cout << sum(10, 15) << endl; // Statement 1
cout << sum(10, 15, 25) << endl; // Statement 2
cout << sum(10, 15, 25, 30) << endl; // Statement 3
}
Example
#include <iostream.h>
int main()
{
int a=10;
int &value=a;
cout << value << endl;
return 0;
}
For example,
#include <iostream.h>
int main()
{
int a=70; // variable initialization
int &b=a;
int &c=a;
cout << "Value of a is :" <<a<<endl;
cout << "Value of b is :" <<b<<endl;
cout << "Value of c is :" <<c<< endl;
return 0;
}
int a = 10;
int &p = a; Reference
int a = 5;
int b = 6;
int &p = a;
int &p = b; // Throw an error of "multiple declaration of reference"
int &q = b; // However it is valid statement,
int *p;
&p = a; //pointer memory address
cout << &p << endl << &a;
In Pointers,
int a = 10;
int *p;
int **q; // It is valid in pointer
p = &a;
q = &p;
#include <iostream.h>
#include <conio.h>
void fun(int x)
{
x = 30; // definition of function
}
int main()
{
int y = 20;
fun(y);
cout << "y = " << y;
return 0;
}
Example
#include <iostream.h>
#include <conio.h>
void func(int &);
void main()
{
int a=10;
cout <<"Value of 'a' is :" <<a<< endl;
func(a);
cout << "Now value of 'a' is :" <<a<<endl;
getch();
Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 18
}
#include <iostream.h>
#include <conio.h>
void main()
{
int x = 20;
fun(&x);
cout << "x = " << x;
getch();
}