Chapter 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Getting Started with C++

Objective

In this chapter learner able to understand:


• Drawbacks of Traditional Programming
• OOP’s Concepts Overview
• Introduction to C++
• Classes and object in C++
• Scope Resolution Operator
• Inline Functions
• Reference Variable
Drawbacks of traditional programming

 Unmanageable Programs
 Problems in modification of data
 Difficulties in implementation
 Focus on function rather than data
Object Oriented Languages

 Focus on Data rather than Procedure


 Programs are divided into what are known as OBJECTS.
 Functions that operate on the data of an object are tied together in the data structure.
 Data is hidden & can not be accessed by external functions.
 New data & functions can be easily added whenever necessary
Object Oriented Concepts
 OOP’s is a programming technique for developing software solutions wherein real
world is represented in terms of objects.
 The fundamental construct of Object-Oriented Programming (OOP) is object which
combines both data structure and behavior as a single entity.

Components of OOP’s
An object-oriented program consists of a number of components, which are as
follows:
Object
Class
Abstraction
Encapsulation
Polymorphism
Inheritance
Object Oriented Languages

Object Based Programming Language


Encapsulation
Data Hiding
Automatic initialization & Clear up of objects
Operator overloading
Ex- Ada, Visual Basic
Object Oriented Programming Language
OBPL Features + Inheritance + Dynamic Binding
Ex- SmallTalk, C++ , Visual C++, Java,C#
C++ Development History
C++ was developed by Bjarne Stroustrup at AT & T Bell Laboratory of USA in 1983.
C++ Development History
C++ was designed to provide Simula’s facilities for program organization together with C’s
efficiency and flexibility for systems programming. It was intended to deliver that to real
projects within half a year of the idea and succeeded.
Year Known as Main Features
1979-83 C with Classes classes,derived classes, public/private access control,
constructors and destructors, friend classes, type checking and
conversion of function arguments,inline functions,default
arguments,overloading of the assignment operator
1982-85 C with Classes Virtual functions, Function name and operator overloading,
to C++ References, Constants (const), User−controlled free−store
memory control, Improved type checking
1985-88 The C++ multiple inheritance, type−safe linkage,better facilities for
Programming user−defined memory management, abstract classes, static
Language(1.0, member functions, protected members (first provided in release
2.0) 1.2)
1998 C++98 ISO working group standardized C++ for the first time
2003- C++03, C+ STL,Containers,Threading,Regular Expression Support,new
11,14 +11, C++14 container classes
A simple Program of C++

#include<iostream.h>
int main()
{
cout<<“CDAC Patna”;
return 0;
}
Classes and Object in C++
 A class is an Abstract Data Type(ADT) in C++.
 A class is extension of the idea of structure used in C. It is a new
way of creating and implementing a data type.
 A class is a way to bind the data and its associated functions
together. It allows the data (and function) to be hidden, if
necessary, from external use.
 The variables declared inside the class are known as data
members and the functions are known as member functions.
 The binding of data and functions together into a single class
type variable is referred as encapsulation .
 An identifier of any class type is called the object of that type.
C++ Classes
In C++ A class may consist Data Member , Member Function, Constructors Destructor
etc.

Decleration Syntax:

class <class-name>
{
access-specifier: data-member1
access-specifier: member-function1
// In the same way it can also contain
Constructor,destructor etc
};
ACCESS SPECIFIER’S IN C++

C++ Provide us Following 3 Access Specifier’s

1.public
2.protected
3.private
Access Modifiers for Class Members

Public:
Accessible by any method of any class within the program.
Protected:
Access limited to the containing class or types derived from
the containing class .
Private:
Access limited to the containing type.
The Scope Resolution Operator

The :: is called the scope resolution operator


the :: operator links a class name with a member name in order to tell the
compiler what class the member belongs to.
However, the scope resolution operator has another related use.it can allow access
to a name in an enclosing scope that is "hidden" by a local declaration of the same
name.
 For example, consider this fragment:
int i; // global i
void fun()
{
int i; // local i
i = 10; // uses local i
::i=20; //uses global i
}
C++ Classes

A Simple Class Example:


class Employee
{
private:
int IDNO;
string NAME;
public:
void setDetails();
void getDetails();
};

To Create Object Of Given Class


Employee e1; //Now e1 is an instance of class employee.
To access member of Employee class:
e1.setDetails();
e1.getDetails();
• In C++ the class variables are known as objects, e1 is
object of type Employee.
• The declaration of an object is similar to that of a variable
of any basic type .The necessary memory space is
allocated to an object at this stage.
• Class specification provides only a template and does not
create any memory space for the objects.
Memory allocation for objects

fun1
Common for all objects
fun2

o1 o2 o3
a a a
b b b
Inline Function

 Whenever a function is called , it takes a lot of extra time in


executing a series of instructions for tasks such as –
jumping to the function,
saving registers,
pushing arguments in to the stack and
returning to the calling function
 Above task will execute always, either the function is small or
big.
 When a function is small, these task become overhead.
 One solution to this problem is to use macro definition( macro
like function) .
Inline Function
• C++ has a different solution to this problem that is 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.
• To make a function inline to the function definition use inline
keyword.
Example: inline double cube (double a)
{
return(a*a*a);
}
c = cube(3.0);
d = cube(2.5 + 1.5);
• Usually the functions are made inline when they
are small enough to be defined in one or two lines.
• Remember that the inline keyword sends a request
, not a command , to the compiler . The compiler
may ignore this request if the function definition is
too long & too complicated . Compiler will treat
inline function as a normal function.
• Some of the situations where inline
expansion may not work.
– if it has a loop, a switch, a goto .
– if function contain static variables.
– if function is recursive.
//A PROGRAM OF INLINE FUNCTION

#include<iostream.h>
inline float multiply(float x, float y )
{
return (x*y);
}
int main()
{
float a = 12.345f;
float b = 9.82f;
cout<<multiply(a,b);
return 0;
}
Making outside function inline

 It is a good practice to define the member functions outside the class.


 We can define a member function outside the class definition and still make it
inline just using the qualifier inline in the line of function definition.
//A PROGRAM OF MAKING OUTSIDE FUNCTION INLINE
#include<iostream.h>
class InlineTest
{
public:
void show();
};
inline void InlineTest::show()
{
cout<< “ Hello,I am Inline “;
}
void main()
{
InlineTest o1;
o1.show();
}
Reference Variable
 C++ Introduces a new kind of variable known as the reference
variable.
 A reference variable provides an alias (nick name ) for a
previously defined variable.
 Both the variable refer to the same data object in the memory .
Hence , change in the value of one will also be reflected in the
value of the other variable.
 A reference variable must be initialized at the time of
declaration.
 This establishes the correspondence between the reference and
the data object which it names.
 A reference , internally works as a const pointer hence once
initialized a reference can not be made to refer to another
variable . Unlike a pointer , reference gets automatically de-
referenced.
Syntax –
data_type &ref_name = var_name ;

Examples –

float total = 100; int a = 5;


float &sum = total; int &b = a;
Thank You

You might also like