Ctuc103 Unit 01
Ctuc103 Unit 01
Include Files
Class Declaration
Member Function
Definitions
Main Function Program
1|Page
A typical C++ program contains four sections. The sections may be placed in
separate code files and then complied jointly or independently.
It is common to organize a program into three separate files.
The class declarations are placed in a header file and the definitions of member
functions are placed in another file. This approach enables the programmers to
separate the abstract specification (class definition) from the implementation
details.
Finally, the main program that uses the class is placed in a third file which includes
the previous two files as well as any other files required.
Fundamental of OOP:
Emphasis is on doing algorithms.
Large programs are divided into functions.
Most of the functions share the global data.
Data move freely around the system from function to function.
Functions transform data from one form to another.
Follows top–down approach in program design.
2|Page
New data and functions can be easily added whenever needed.
Follows bottom–up approach in the program design.
Characteristics of OOP:
Classes
Objects
Data Abstraction and Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
Classes
A class is a way to bind the data and its associated functions together. It allows the data
and functions to be hidden from external use. It is a user-defined data type and behaves
like a built–in type. It is also called the Abstract Data Types(ADT) because the classes use
the concept of data abstraction.
3|Page
Once a class has been defined, we can create any number of objects of that class. Each
object is associated with the data of the type class with which they are created. Thus a
class is a collection of objects of similar type.
Eg. roll_no and name are the members of the class STUDENT. It can be declared as follows:
class Student
{
private:
int roll_no;
char name[10];
public:
void getdata();
void putdata();
}
Objects:
Objects are the basic run-time entities in an Object–oriented system. The programming
problem is analysed in terms of objects and the nature of communication between them.
When the program is executed, the objects interact by sending messages to one another.
Example: If “customer” and “account” are two objects in a program, then the customer
object may send a message to the account object requesting the bank balance.
Each object contains data and code to manipulate the data. Objects can interact without
having to know the details of each other’s data or code.
Example: If Student is the class then we can create an object of type student as:
void main()
{
Student s1;
}
4|Page
Abstraction means representing the essential features without including background
details. Classes use the concept of data abstraction and are defined as a list of attributes.
They encapsulate all the essential properties of the objects that are to be created. The
attributes are called data members because they hold the information. The functions that
operate on these data are called member functions.
Inheritance:
The mechanism of deriving a new class from an old class is called Inheritance or
Derivation. In OOP, the concept of inheritance provides the idea of reusability. This
means that we can add additional features to an existing class without modifying it. This
is possible by deriving a new class from the existing one. The new class will have the
combined features of both classes. The old class is called the base class and the new class
is called the derived class. Each derived class defines only those features that are unique
to it.
Polymorphism:
It is a Greek term, that means the ability to take more than one form. An operation may
provide different behaviors in different situations. The behavior depends upon the types
of data used in the operation.
Eg. Consider the operation of addition. For two numbers, the operation will generate a
sum. If the operands are strings, then the operation would produce a third string by
concatenation. The process of making an operator provide different behavior in different
situations is called operator overloading. Polymorphism is extensively used in
implementing inheritance.
Inline function:
One of the objectives of using functions in a program is to save memory space which
provides an advantage when a function is likely to be called many times.
However, every time a function is called, it takes a lot of extra time to execute a series of
instructions for tasks such as jumping to the function, saving registers, pushing
arguments into a stack, and returning to the calling function.
When a function is small, then a considerable percentage of execution time may be spent.
5|Page
C++ provides a solution for this. To eliminate the cost of calls to small functions, C++
provides a new feature called Inline Function.
An inline function is a function that is expanded in line when it is invoked. That is compiler
replaces the function call with the corresponding function code.
Syntax: Example:
inline function – header inline int test(int a)
{ {
Function body return (a * a);
} }
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 the function contains static variables.
4. If inline functions are recursive.
Example:
inline int mul (int x, int y)
{
return x*y;
}
inline int div (int x, int y)
{
return x/y;
}
6|Page
int main ()
{
int a, b;
a=50; b=25;
cout<<"\nMultiplication : "<<mul (a, b);
cout<<"\nDivision : "<<div (a, b);
return 0;
}
Output would be as follows:
Multiplication : 1250
Division : 2
Call by Value:
void swap(int , int);
int main()
{
int a, b;
cout<<”Enter the value for A : “;
cin>>a;
cout<<”Enter the value for B : “;
cin>>b;
swap(a,b);
cout<<”\nAfter interchange in main program”;
cout<<”\nValue of a is : “<<a;
cout<<”\nValue of b is : “<<b;
return 0;
}
void swap(int a,int b)
{
int c;
c=a;
7|Page
a=b;
b=c;
cout<<”\nIn subprogram.”;
cout<<”\nValue of a is : “<<a;
cout<<”\nValue of b is : “<<b;
}
Call by Reference:
void swap(int * ,int *);
int main()
{
int a,b;
cout<<”Enter the value for A : “;
cin>>a;
cout<<”Enter the value for B : “;
cin>>b;
swap(&a,&b);
cout<<”\nAfter interchange in main program”;
cout<<”\nValue of a is : “<<a;
cout<<”\nValue of b is : “<<b;
return 0;
}
void swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
cout<<”\nIn subprogram.”;
cout<<”\nValue of a is : “<<*a;
cout<<”\nValue of b is : “<<*b;
}
Default Arguments:
C++ allows us to call a function without specifying its arguments. In such cases, the
function assigns a default value to the parameter that does not have a matching
argument 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.
8|Page
Example:
float amount( float pamt, int year, float rate = 2.5);
The above prototype declares a default value of 2.5 to the argument rate. Then a function
call like
value = amount (1000,2); // one argument missing
passes the value 1000 to pamt and 2 to year and then lets the function use a default value
of 2.5 for rate.
Const Arguments:
An argument to the function can be declared as constant as follows:
int strlen(const char *p);
int length(const string &s);
The qualifier tells the compiler that the function should not modify the argument. The
compiler will generate an error when this condition is violated. This type of declaration
is significant only when we pass the arguments by reference or pointer.
9|Page