Lec 1
Lec 1
Lec 1
USING C++
BENNETT
UNIVERSITY
www.bennett.edu.in www.bennett.edu.in
CONTENTS
1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
www.bennett.edu.in
CONTENTS
OBJECTS
Objects are the basic run-time entities in an object-oriented system. They may represent a person, a
place, a bank account, a table of data or any item that the program must handle.
The fundamental idea behind object-oriented approach is to combine both data and function into a single
unit and these units are called objects.
www.bennett.edu.in
CONTENTS
CLASS :
A group of objects that share common properties for data part and some program part are
collectively called as class.
In C ++ a class is a new data type that contains member variables and member functions that
operate on the variables.
DATA ABSTRACTION :
Abstraction refers to the act of representing essential features without including the
background details or explanations. Classes use the concept of abstraction and are defined as
size, width and cost and functions to operate on the attributes.
DATA ENCAPSALATION :
The wrapping up of data and function into a single unit (called class) is known as
encapsulation. The data is not accessible to the outside world and only those functions which
are wrapped in the class can access it. These functions provide the interface between the
objects data and the program.
www.bennett.edu.in
CONTENTS
INHERITENCE :
Inheritance is the process by which objects of one class acquire the properties of another class.
In the concept of inheritance provides the idea of reusability. This mean that we can add
additional features to an existing class with out modifying it. This is possible by designing a
new class will have the combined features of both the classes.
POLYMORPHISIM:
Polymorphism means the ability to take more than one form. An operation may exhibit different
instance. The behavior depends upon the type of data used in the operation.
A language feature that allows a function or operator to be given more than one definition. The
types of the arguments with which the function or operator is called determines which definition
will be used.
www.bennett.edu.in
CONTENTS
DYNAMIC BINDING :
Binding refers to the linking of a procedure call to the code to the executed in response to the
call. Dynamic binding means the code associated with a given procedure call is not known
untill the time of the call at run-time. It is associated with a polymorphic reference depends
upon the dynamic type of that reference.
www.bennett.edu.in
CONTENTS
MESSAGE PASSING :
An object-oriented program consists of a set of objects that communicate with each other.
A message for an object is a request for execution of a procedure and therefore will invoke a function
(procedure) in the receiving object that generates the desired result. Message passing involves specifying
the name of the object, the name of the function (message) and information to be sent.
www.bennett.edu.in
CONTENTS
Structure of C++ Program
# include<iostream.h>
class person void display()
{ {
char name[30]; cout<<”\n name:”<<name;
int age; cout<<”\n age:”<<age;
public: }
void getdata(void);
void display(void); int main( )
}; {
void person :: getdata ( void ) person p;
{ p.getdata();
cout<<”enter name”; p.display();
cin>>name; return(0);
cout<<”enter age”; }
cin>>age;
}
www.bennett.edu.in
CONTENTS
TOKENS:
The smallest individual units in program are known as tokens. C++ has the following tokens.
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
www.bennett.edu.in
CONTENTS
www.bennett.edu.in
CONTENTS
www.bennett.edu.in
CONTENTS
Example:
enum shape { circle,square,triangle}
enum colour{red,blue,green,yellow}
enum position {off,on}
www.bennett.edu.in
CONTENTS
SYMBOLIC CONSTANT:
There are two ways of creating symbolic constants in c++.
1. using the qualifier const.
2. defining a set of integer constants using enum keywords.
In both C and C++, any value declared as const can’t be modified by the program in any way.
In C++, we can use const in a constant expression. Such as
const int size = 10 ;
char name (size) ;
www.bennett.edu.in
CONTENTS
REFERENCE VARIABLES:
C++ interfaces a new kind of variable known as the reference variable. A references variable provides an
alias.(alternative name) for a previously defined variable. For example ,if we make the variable sum a
reference to the variable total, then sum and total can be used interchangeably to represent the variable.
A reference variable is created as follows:
Syntax: Datatype & reference –name=variable name;
Example:
float total=1500;
float &sum=total;
Here sum is the alternative name for variables total, both the variables refer to the same data object in the
memory .
A reference variable must be initialized at the time of declaration .
Note that C++ assigns additional meaning to the symbol & here & is not an address operator .The notation
float & means reference to float.
Example:
int n[10];
int &x=n[10];
char &a=’\n’;
www.bennett.edu.in
CONTENTS
OPERATORS IN C++ :
C++ has a rich set of operators. All C operators are valid in C++ also. In addition. C++
introduces some new operators.
<< insertion operator
>> extraction operator
:: scope resolution operator
: :* pointer to member declarator
* pointer to member operator
.* pointer to member operator
Delete memory release operator
Endl line feed operator
New memory allocation operator
Setw field width operator
www.bennett.edu.in
CONTENTS
CONTROL STRUCTURES:
The if statement:
Simple if statement:
if (condition)
{
Action;
}
If.. else statement
If (condition)
Statment1
Else
Statement2
www.bennett.edu.in
The switch statement
This is a multiple-branching statement where, based on a condition, the control is transferred to one of the
many possible points;
Switch(expr)
{
case 1:
action1;
break;
case 2:
action2;
break;
..
..
default:
message
}
www.bennett.edu.in
The while statement:
Syn:
While(condition)
{ The for loop:
Statements for(expression1;expression2;expression3)
} {
Statements;
Statements;
The do-while statement: }
Syn:
do
{
Statements
} while(condition);
www.bennett.edu.in
FUNCTION IN C++ :
The main( ) Functon ;
ANSI does not specify any return type for the main ( ) function which is the starting point for the
execution of a program . The definition of main( ) is :-
main()
{
//main program statements
}
www.bennett.edu.in
INLINE FUNCTION:
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 inline when it is invoked .That is the compiler replaces the function call
with the corresponding function code.
The inline functions are defined as follows:-
inline function-header
{
function body;
}
Example: inline double cube (double a)
{
return(a*a*a);
}
The above inline function can be invoked by statements like
c=cube(3.0);
d=cube(2.5+1.5);
remember that the inline keyword merely sends a request, not a command to the compliler. The compiler may
ignore this request if the function definition is too long or too complicated and compile the function as a
normal function.
www.bennett.edu.in
DEFAULT ARGUMENT :-
C++ allows us to call a function without specifying all its arguments. In such cases, the
function assigns a default value to the parameter which does not have a matching arguments in
the function call. Default values are specified when the function is declared. The compiler
looks at the prototype to see how many arguments a function uses and alerts the program for
possible default values.
Example: float amount (float principle, int period ,float rate=0.15);
The default value is specified in a manner syntactically similar to a variable initialization .The
above prototype declares a default value of 0.15 to the argument rate. A subsequent function
call like
value=amount(5000,7); //one argument missing
passes the value of 5000 to principle and 7 to period and then lets the function, use default
value of 0.15 for rate.
www.bennett.edu.in
FUNCTION OVERLOADING:
Overloading refers to the use of the same thing for different purposes . C++ also permits
overloading functions .This means that we can use the same function name to creates functions
that perform a variety of different tasks. This is known as function polymorphism in oops.
Using the concepts of function overloading , a family of functions with one function name but
with different argument lists in the functions call .The correct function to be invoked is
determined by checking the number and type of the arguments but not on the function type.
For example an overloaded add() function handles different types of data as shown below.
//Declaration
www.bennett.edu.in
//function call
www.bennett.edu.in
PROGRAM
#include<iostream.h> double volume( double r, int h)
int volume(double,int); {
double volume( double , int ); return(3.1416*r*r*h); //cylinder
double volume(longint ,int ,int); }
main( ) long volume (longint 1, int b, int h)
{ {
cout<<volume(10)<<endl; return(1*b*h); //cylinder
cout<<volume(10)<<endl; cout<<volume(10)<<endl; }
} output:- 1000
int volume( ini s) 157.2595
{ 112500
return (s*s*s); //cube
}
www.bennett.edu.in
CLASS:-
Class is a group of objects that share common properties and relationships. In C++, a class is
a new data type that contains member variables and member functions that operates on the
variables. A class is defined with the keyword class. It allows the data to be hidden, if
necessary, from external use. When we defining a class, we are creating a new abstract data
type that can be treated like any other built in data type.
www.bennett.edu.in
Syntax:-
class class-name
{
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
www.bennett.edu.in
CREATING OBJECTS:
Once a class has been declared we can create variables of that type by using the class name.
Example:
item x;
creates a variables x of type item. In C++, the class variables are known as objects. Therefore x
is called an object of type item.
item x, y ,z also possible.
class item
{
-----------
-----------
-----------
}x ,y ,z;
would create the objects x ,y ,z of type item.
www.bennett.edu.in
ACCESSING CLASS MEMBER:
The private data of a class can be accessed only through the member functions of that class. The
main() cannot contains statements that the access number and cost directly.
Syntax:
object name.function-name(actual arguments);
Example:- x. getdata(100,75.5);
It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata()
function .
similarly the statement
x. putdata ( ); //would display the values of data members.
www.bennett.edu.in
DEFINING MEMBER FUNCTION:
Member can be defined in two places
• Outside the class definition
• Inside the class function
www.bennett.edu.in
The member ship label class-name :: tells the compiler that the function function-name belongs to
the class class-name . That is the scope of the function is restricted to the class-name specified in
the header line. The :: symbol is called scope resolution operator.
Example:
void item :: getdata (int a , float b )
{
number=a;
cost=b;
}
void item :: putdata ( void)
{
cout<<”number=:”<<number<<endl; cout<<”cost=”<<cost<<endl;
}
www.bennett.edu.in
www.bennett.edu.in
INSIDE THE CLASS DEF1NATION:
Another method of defining a member function is to replace the function declaration by the
actual function definition inside the class .
Example:
class item
{
Intnumber; float cost;
public:
void getdata (int a ,float b);
void putdata(void)
{
cout<<number<<endl; cout<<cost<<endl;
}
};
www.bennett.edu.in
Write a simple program using class in C++ to input subject mark and prints it.
class marks
void marks: :displaydata()
{
{
private :
cout<<”Ist subject
int ml,m2;
mark:”<<ml<<endl ;
public:
cout<<”2nd subject mark:”<<m2;
void getdata();
}
void displaydata();
void main()
};
{
void marks: :getdata()
clrscr();
{
marks x;
cout<<”enter 1st subject mark:”;
x.getdata();
cin>>ml;
x.displaydata();
cout<<”enter 2nd subject mark:”;
}
cin>>m2;
}
www.bennett.edu.in
NESTING OF MEMBER FUNCTION;
Although it is a normal practice to place all the data items in a private section and all the
functions in public, some situations may require contain functions to be hidden from the
outside calls. Tasks such as deleting an account in a customer file or providing increment to
and employee are events of serious consequences and therefore the functions handling such
tasks should have restricted access. We can place these functions in the private section.
A private member function can only be called by another function that is a member of its class.
Even an object can not invoke a private function using the dot operator.
www.bennett.edu.in
Thank You
www.bennett.edu.in