KEMBAR78
C++ programming introduction | PPTX
OOPS- C++ Programming
FY.BSc. IT
Sem-II
No. C C++
1) C follows the procedural style
programming.
C++ is multi-paradigm. It
supports both procedural and
object oriented.
2) Data is less secured in C. In C++, you can use modifiers for
class members to make it
inaccessible for outside users.
3) C follows the top-down approach. C++ follows the bottom-up
approach.
4) C does not support function
overloading.
C++ supports function
overloading.
5) In C, you can't use functions in
structure.
In C++, you can use functions in
structure.
6) C does not support reference
variables.
C++ supports reference variables.
7) In C, scanf() and
printf() are mainly used for
input/output.
C++ mainly uses stream cin and
cout to perform input and output
operations.
8) Operator overloading is
not possible in C.
Operator overloading is possible in
C++.
9) C programs are divided
into procedures and
modules
C++ programs are divided
into functions and classes.
10) C does not provide the
feature of namespace.
C++ supports the feature of
namespace.
11) Exception handling is not
easy in C. It has to perform
using other functions.
C++ provides exception handling
using Try and Catch block.
12) C does not support the
inheritance.
C++ supports inheritance.
#include<iostream.h>
#include<conio.h>
class calculator
{
Int a,b;
public:
void getdata(); for input
void add(); for addition of two number
void sub(); for subtraction of two number
};
void calculator::getdata() define member function
{
cout<<"Enter the First Number:";
cin>>a;
cout<<“Enter the Second Number:";
cin>>b;
}
void calculator::add() define member function
{
cout<<“Addition of A & B is="<<a+b<<“n”;
}
void calculator::sub() define member function
{
cout<<“Subtraction of A & B is="<<a-b<<“n”;
}
void main()
{
clrscr();
calculator c;
c.getdata();
c.add();
c.sub();
getch();
Class and Object
Member Functions in Classes
There are 2 ways to define a member function:
• Inside class definition
• Outside class definition
To define a member function outside the class
definition we have to use the scope resolution ::
operator along with class name and function
name.
Inside the Class
class student
{
public:
int id;
// getdata() is not defined inside class definition
void getdata();
//getdata() is defined inside class definition
void getdata()
{
cout << "student id is: " << id;
} };
Outside the class
// Definition of getdata using scope resolution operator ::
void student::getdata()
{
cout << “Data is: " << sid;
}
Object Oriented Programming Concepts
• Objects
• Classes
• Abstraction & Encapsulation
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
Objects
Objects are the basic unit of OOP. They are
instances of class, which have data members
and uses various member functions to perform
tasks.
When a program is executed the objects interact
by sending messages to one another.
Each object contains data and code to manipulate
the data. Objects can interact without having to
know details of each other’s data or code.
Class
Class can also be defined as user defined data type
but it also contains functions in it. So, class is
basically a blueprint for object. It declare & defines
what data variables the object will have and what
operations can be performed on the class's object.
Example: Consider the Class of Cars. There may
be many cars with different names and brand but all
of them will share some common properties like all
of them will have 4 wheels, Speed Limit, Mileage
range etc. So here, Car is the class and wheels,
speed limits, mileage are their properties.
Abstraction
Abstraction refers to showing only the essential
features of the application and hiding the details. In
C++, classes can provide methods to the outside
world to access & use the data variables, keeping
the variables hidden from direct access, or classes
can even declare everything accessible to everyone,
or maybe just to the classes inheriting it. This can be
done using access specifiers.
Encapsulation
It can also be said data binding. Encapsulation is all
about binding the data variables and functions
together in class.
Polymorphism
The word polymorphism means having many
forms. In simple words, we can define
polymorphism as the ability of a message to
be displayed in more than one form.
A person at the same time can have different
characteristic. Like a man at the same time is a
father, a husband, an employee. So the same
person posses different behaviour in different
situations. This is called polymorphism.
Example
Inheritance
The capability of a class to derive properties and
characteristics from another class is called Inheritance.
Reusability: Inheritance supports the concept of
“reusability”, i.e. when we want to create a new class
and there is already a class that includes some of the
code that we want, we can derive our new class from
the existing class. By doing this, we are reusing the fields
and methods of the existing class.
Sub Class: The class that inherits properties from
another class is called Sub class or Derived Class.
Super Class: The class whose properties are inherited by
sub class is called Base Class or Super class.
Example:
Dog, Cat, Cow can be Derived Class of Animal Base Class.
Dynamic Binding: In dynamic binding, the code to
be executed in response to function call is decided at
runtime. C++ has virtual functions to support this.
Message Passing: Objects communicate with one
another by sending and receiving information to
each other. A message for an object is a request for
execution of a procedure and therefore will invoke a
function in the receiving object that generates the
desired results. Message passing involves specifying
the name of the object, the name of the function
and the information to be sent.
Example: <objectname.memberfunction>

C++ programming introduction

  • 1.
  • 2.
    No. C C++ 1)C follows the procedural style programming. C++ is multi-paradigm. It supports both procedural and object oriented. 2) Data is less secured in C. In C++, you can use modifiers for class members to make it inaccessible for outside users. 3) C follows the top-down approach. C++ follows the bottom-up approach. 4) C does not support function overloading. C++ supports function overloading. 5) In C, you can't use functions in structure. In C++, you can use functions in structure. 6) C does not support reference variables. C++ supports reference variables.
  • 3.
    7) In C,scanf() and printf() are mainly used for input/output. C++ mainly uses stream cin and cout to perform input and output operations. 8) Operator overloading is not possible in C. Operator overloading is possible in C++. 9) C programs are divided into procedures and modules C++ programs are divided into functions and classes. 10) C does not provide the feature of namespace. C++ supports the feature of namespace. 11) Exception handling is not easy in C. It has to perform using other functions. C++ provides exception handling using Try and Catch block. 12) C does not support the inheritance. C++ supports inheritance.
  • 6.
    #include<iostream.h> #include<conio.h> class calculator { Int a,b; public: voidgetdata(); for input void add(); for addition of two number void sub(); for subtraction of two number };
  • 7.
    void calculator::getdata() definemember function { cout<<"Enter the First Number:"; cin>>a; cout<<“Enter the Second Number:"; cin>>b; } void calculator::add() define member function { cout<<“Addition of A & B is="<<a+b<<“n”; }
  • 8.
    void calculator::sub() definemember function { cout<<“Subtraction of A & B is="<<a-b<<“n”; } void main() { clrscr(); calculator c; c.getdata(); c.add(); c.sub(); getch();
  • 9.
  • 10.
    Member Functions inClasses There are 2 ways to define a member function: • Inside class definition • Outside class definition To define a member function outside the class definition we have to use the scope resolution :: operator along with class name and function name.
  • 11.
    Inside the Class classstudent { public: int id; // getdata() is not defined inside class definition void getdata(); //getdata() is defined inside class definition void getdata() { cout << "student id is: " << id; } };
  • 12.
    Outside the class //Definition of getdata using scope resolution operator :: void student::getdata() { cout << “Data is: " << sid; }
  • 13.
    Object Oriented ProgrammingConcepts • Objects • Classes • Abstraction & Encapsulation • Polymorphism • Inheritance • Dynamic Binding • Message Passing
  • 16.
    Objects Objects are thebasic unit of OOP. They are instances of class, which have data members and uses various member functions to perform tasks. When a program is executed the objects interact by sending messages to one another. Each object contains data and code to manipulate the data. Objects can interact without having to know details of each other’s data or code.
  • 17.
    Class Class can alsobe defined as user defined data type but it also contains functions in it. So, class is basically a blueprint for object. It declare & defines what data variables the object will have and what operations can be performed on the class's object. Example: Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.
  • 18.
    Abstraction Abstraction refers toshowing only the essential features of the application and hiding the details. In C++, classes can provide methods to the outside world to access & use the data variables, keeping the variables hidden from direct access, or classes can even declare everything accessible to everyone, or maybe just to the classes inheriting it. This can be done using access specifiers. Encapsulation It can also be said data binding. Encapsulation is all about binding the data variables and functions together in class.
  • 19.
    Polymorphism The word polymorphismmeans having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.
  • 20.
  • 21.
    Inheritance The capability ofa class to derive properties and characteristics from another class is called Inheritance. Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. Super Class: The class whose properties are inherited by sub class is called Base Class or Super class.
  • 22.
    Example: Dog, Cat, Cowcan be Derived Class of Animal Base Class.
  • 23.
    Dynamic Binding: Indynamic binding, the code to be executed in response to function call is decided at runtime. C++ has virtual functions to support this. Message Passing: Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent. Example: <objectname.memberfunction>