2. Introduction to C++ programming:
Object Oriented Programming Concepts, Structured Vs OOP.
Classes and Objects:
Class Definition, Objects, Class Scope and Accessing Members.
Constructors : Default Constructor, Parameterized Constructor,
Constructor initialization list, Copy Constructor and
Destructors.
3. History of C++
Extension of C
Early 1980s: Bjarne Stroustrup (Bell Laboratories)
Originally named “C with Classes”.
Provides capabilities for object-oriented programming.
• Objects: Reusable Software Components
– Model items in real world
• Object-Oriented Programs
– Easy to understand, correct and modify
Hybrid language
• C-like style
• Object-Oriented style
• Both
4. Object Oriented Programming Concepts
OOP is a programming style that is focused on objects.
Object Oriented Programming Concepts
– Classes
– Objects
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
– Message Passing
5. A class is collection of objects of similar type or it is a template.
Ex: fruit mango;
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 a
programming language.
Objects are instances of the type class.
Objects are the basic run-time entities in an object-oriented system.
class object
Class and Object:
6. Encapsulation:
The wrapping up of data and functions into a single unit ( called class)
is known as encapsulation.
That is the data and method are given in the class definition.
Data encapsulation is the most striking features of a class.
Abstraction:
Abstraction refers to the act of representing essential features without
including the background details or explanations.
The access modifiers in C++ or any OOP language, provides abstraction.
If a variable is declared as private, then other classes cannot access it.
7. Polymorphism: Poly – Many Morph – Form
Polymorphism is the characteristic that enables an entity to co exist in
more than one form.
Ex: It allows the single method to perform different actions based on the
parameters.
C++ supports function overloading and operator overloading to
implement polymorphism
Inheritance:
Inheritance is the process by which objects of one class acquire the
properties of another class. The concept of inheritance provides the
reusability.
A class can inherit one or more classes. Inherited class is called as parent
class or super class or base class. Class that inherits a parent class is
called as child class or sub class or derived class.
8. Drawing Object
Multiple-Point Object Two-Point Object
Polygon Curve Rectangle Line
Example for Inheritance
Example for Polymorphism
Shape
Draw()
Circle object
Draw()
Box object
Draw()
Triangle
Draw()
9. Message passing:
Set of objects that communicate with each other.
1. Create the classes that define objects and their behavior.
2. Creating the objects for that class.
3. Establish the communication among objects.
Example: account. Balance_enquiry(accountno);
Object message information
Dynamic Binding:
When a method is called within a program, it associated with the
program at run time rather than at compile time is called dynamic
binding.
10. Structured Oriented Programming:
Main program
Function-5
Function-2 Function-3
Function-4
Function-1
Function-6 Function-7 Function-8
Global data Global data
Function-1
Local data
Function-2
Local data
Function-3
Local data
Relationship of Data &
Functions in Structured
Programming
Program is divided into functions, every function has its own data and global
data
11. OOP decompose the problem into number of entities called objects and it
contains the data and functions.
Object A Object B
Data
Functions
Data
Functions
Data
Functions
Object C
Communication
Object Oriented Programming
12. Characteristics of Structure-Oriented Programming
Emphasis is on doing things.
Large programs are divided into smaller programs known as functions.
Most of the functions share global data.
Data move openly around the system from function to function
Functions transform data from one form to another
Employs top-down approach in program design
Characteristics of Object-Oriented Programming
Emphasis is 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 and cannot be accessed by external functions.
Objects may communicate with each other through functions.
Employs bottom-up approach in program design
New data and functions can be easily added whenever necessary.
Structured Vs OOP
13. Top Down approach:
– A Single module will be split into several smaller modules
– General to Specific
Bottom Up approach:
– Lot of small modules will be grouped to form a single large module
– Specific to General
If the requirements are clear at the first instance we can go for Top Down
approach.
In circumstances where the requirements may keep on adding, we go for
Bottom Up approach.
14. Benefits of OOP
Through inheritance, we can eliminate redundant code and extend the use of
existing classes.
The principle of data hiding helps the programmer to build secure programs
that cannot be invaded by code in the parts of the program.
It is easy to partition the work in a project based on objects.
Object oriented system easily upgraded from small to large systems.
Software complexity can be easily managed.
Applications of OOP
Real-time systems.
Object-Oriented Databases.
Neural Networks and Parallel Programming.
Decision Support and Office Automation Systems.
15. I/O in C++
Since C++ is a superset of C, all of the C I/O functions such as printf and
scanf which are found in the stdio.h header file, are still valid in C++.
C++ provides an alternative with the new stream input/output features by
including “iostream.h”.
Several new I/O objects available when you include the iostream header file.
Two important ones are:
cin // Used for keyboard input
cout // Used for screen output
Both cin and cout can be combined with other member functions for a wide
variety of special I/O capabilities in program applications.
16. Since cin and cout are C++ objects, they are somewhat "intelligent":
They do not require the usual format strings and conversion specifications.
They do automatically know what data types are involved.
They do not need the address operator, &.
They do require the use of the stream extraction (>>) and insertion (<<) operators.
Example with cin and cout:
// program to find average of two numbers
#include<iostream.h>
void main()
{
float n1,n2,avg;
cout<<”Enter two valuesn”;
cin>>n1>>n2;
avg = (n1+n2)/2;
cout<<”nAverage is “<<avg;
}
17. Creating Classes:
The class is basis for the OOP. The class is used to define the nature of
an object, and also it is basic unit for the encapsulation.
The class combines the data and its associated functions together. It allows
the data to be hidden.
The keyword class is used to create a class.
Class has two parts:
1. Data members declaration
2. Prototype of member function declarations
Note: Data members can’t be initialized with in the class. They can be
initialized using member functions of that class.
19. Syntax of declaring a class:
class class-name {
private data and functions;
access-specifier:
data and functions;
access-specifier:
data and functions;
// ...
access-specifier:
data and functions;
}object-list;
Class should enclose both data declaration and function declaration part between
curly parenthesis and class definition should be ended with semicolon.
Members (data members and member functions) are grouped under access specifiers,
namely private, public and protected, which define the visibility of members.
The object-list is optional. If present, it declares objects of the class
20. Creating objects
The process of creating objects of a class is called class instantiation.
Object is instance of the class.
Once the class is created we can create any number of objects.
class class-name
{
------;
------;
}object-list;
class student
{
------;
------;
}s1,s2,s3;
class_name object-name1,object-name2,object-name3;
student s1, s2, s3;
Syntax1: Example1:
Syntax2:
Example2:
21. Accessing class members:
Once an object of a class has been created, then we can access the members
of the class. This is achieved by using the member access operator, dot(.).
Syntax:
Object-Name.DataMember;
Object-Name.Memberfunction(arguments);
Example:
x.getdata(10,20);
x.number;
Here x.number accessing is illegal, if number is private data member of the
class. Private data members are accessed only through the member functions
not directly by objects.
22. Defining the member functions:
We can define the function in two ways:
1. Outside the class definition 2. Inside the class definition
Outside the class definition: Use the Scope Resolution Operator :: to define a
member function outside of the class.
:: Scope Resolution Operator tells the compiler that to which class the specified
function belongs to.
Syntax:
Return_type class-name:: function-name(argmlist)
{
----
----
}
Inside the class definition: We can directly define a member function inside of the
class without using Scope Resolution Operator.
23. Structure of C++ program:
Include files
Class declaration
Member functions definitions
Main function program
24. #include<iostream.h>
#include<string.h>
class student
{
private:
int rollno;
char name[30];
public:
void setdata(int rn, char *n)
{
rollno=rn;
strcpy(name, n);
}
void putdata()
{
cout<<"RollNumber="<<rollno<<" ";
cout<<"Name="<<name<<"n";
}
};
void main()
{
student s1,s2;
s1.setdata(2361,"RAMU");
s2.setdata(2362,"VENKAT");
s1.putdata();
s2.putdata();
}
RollNumber=2361 Name=RAMU
RollNumber=2362Name=VENKAT
//Program for implementing member functions inside of a class
25. #include<iostream.h> #include<string.h>
class student
{
private:
int rollno;
char name[30];
public:
void setdata(int rn, char *n)
void putdata()
};
void student :: setdata(int rn, char *n)
{
rollno=rn;
strcpy(name, n);
}
void student :: putdata()
{
cout<<“RollNumber=“<<rollno<<“ ”;
cout<<“Name=“<<name<<“n”;
}
void main()
{
student s1,s2;
s1.setdata(2361,"RAMU");
s2.setdata(2362,"VENKAT");
s1.putdata();
s2.putdata();
}
RollNumber=2361 Name=RAMU
RollNumber=2362 Name=VENKAT
//Program for implementing member functions outside of a class using
Scope Resolution Operator.
26. Class Scope:
It tells in which parts of the program (or in which functions), the class
declaration can be used.
Class scope may be local or global.
Local:
If class is declared inside the function, then objects can be created inside
that function only.
Global:
If class is declared outside the functions, then object can be created in any
function.
27. #include<iostream.h>
#include<conio.h>
void print() {
class point
{
int a;
public:
void getdata() {
cout<<"Enter an integer number: ";
cin>>a;
}
void display() {
cout<<"Entered number is: "<<a;
}
};
point p;
p.getdata();
p.display();
}
void main()
{
clrscr();
print();
getch();
}
//Example program for class inside a member
function
28. #include<iostream.h>
#include<conio.h>
class point
{
int a;
public:
void getdata()
{
cout<<"nEnter value of a :";
cin>>a;
}
void display( )
{
cout<<"nValue of a is: "<<a;
}
};
void print() {
point p;
p.getdata();
p.display();
}
void main() {
clrscr();
print();
point m;
m.getdata();
m.display();
getch();
}
//Example program for class outside a member function
29. The following are the access specifiers:
1. Private 2. Public 3. Protected
Default access specifier is private.
Access Specifiers
Private:
Private members of a class have strict access control.
Only the member functions of the same class can access these members.
They prevents the accidental modifications from the outside world.
Example:
class person
{
private:
int age;
int getage();
};
A private member functions is only called by the member function of the
same class . Even an object cannot invoke a private function using the dot
operator.
void main(){
person p1;
p1.age=5; // error
p1.getage(); // error
}
int person::getage(){
age=22; //correct
}
30. Public:
All members declared with public can have access to outside of the class
without any restrictions.
Example:
class person
{
public:
int age;
int getage();
};
int person :: getage(){
age=10; //correct
}
void main(){
person p1;
p1.age=20;//correct
cout<<p1.age;
p1.getage(); // correct
}
31. Protected:
Similar to the private members and used in inheritance.
The members which are under protected, can have access to the members
of its derived class.
Example: class A
{
private:
//members of A
protected:
//members of A
public:
//members of A
};
class B: public A //Here class B can have access on
{ protected data of its parent class as
//members of B well as public data.
};
32. Constructors:
A constructor is a special member function whose task is to initialize the
objects of its class.
It is special because its name is the same as the class name.
The constructor is invoked whenever an object of its associated class is
created.
Syntax:
class Test
{
int m, n;
public:
Test();
//constructor declaration
----------
----------
};
Test :: Test() //constructor definition
{
m=0;
n=0;
}
void main(){
Test t; //constructor is invoked
//after creation of object t
}
33. Characteristics of Constructors:
Constructors should be declared in the public section.
They are invoked automatically when the objects are created.
They do not have return types, not even void and therefore, they cannot
return values.
They cannot be inherited, though a derived class can call the base class
constructor.
Like other C++ functions, they can have default arguments.
We cannot refer to their address.
Constructors / Destructors cannot be const.
They make ‘implicit calls’ to the operators new and delete when memory
allocation/de-allocation is required.
34. Types of Constructors:
1) Default Constructor 2) Parameterized Constructor
3) Copy Constructor
Default constructor:
Constructor without parameters is called default constructor.
#include<iostream.h>
class point {
int a;
public:
point( ) { //Default constructor
a=1000;
}
void display( ){
cout<<”a value is “<<a;
}
};
void main( ) {
point p; //constructor is invoked after creation of object p
p.display();
}
35. Parameterized Constructor:
Constructor which takes parameters is called Parameterized Constructor.
Example:
#include<iostream.h>
class A
{
int m, n;
public:
A (int x, int y); // parameterized constructor
};
A :: A (int x, int y)
{
m=x; n=y;
cout<<m<<n;
}
void main(){
A obj(10,20); or A(10,20);
}
36. Copy constructor:
Copy Constructor is used to create an object that is a copy of an existing
object.
Copy constructor is a member function which is used to initialize an object
from another object.
By default, the compiler generates a copy constructor for each class.
Syntax:
class-name (class-name &variable)
{
-----;
-----;
};
You can call or invoke copy constructor in the following way:
class obj2(obj1);
or
class obj2 = obj1;
37. #include<iostream.h>
class point {
int a;
public:
point( ) { //Default Constructor
a=1000;
}
point(int x) { //Parameterized Constructor
a = x;
}
point(point &p) { //Copy Constructor
a = p.a;
}
void display( )
{
cout<<”a value is “<<a;
}
};
void main( )
{
point p1;
point p2(500);
point p3(p1);
p1.display();
p2.display();
p3.display();
}
//Example for Copy Constructor
38. Destructors
Destructors are used to de-allocate memory for a class object and its class
members when the object is destroyed.
A destructor is called for a class object when that object passes out of scope or
is explicitly deleted.
A destructor is a member function with the same name as its class prefixed
by a ~ (tilde).
A destructor takes no arguments and has no return type.
Destructors cannot be declared const or static.
A destructor can be declared virtual or pure virtual.
If no user-defined destructor exists for a class, the compiler implicitly declares
a destructor.
Syntax:
class X
{
public: // Constructor for class X
X();
~X(); // Destructor for class X
};
39. include<iostream.h>
class des {
int a;
public:
des( ) { //Default constructor
a=1000;
}
~des( ) { //Destructor
cout<<”Destructor called”;
}
void display( ) {
cout<<endl<<”a value is “<<a;
}
};
void main( ) {
des p; //Constructor is invoked after creation of object p
p.display();
}
//Example program for Default Constructor