Introduction to C++
BY SANHITA GHOSAL (MUKHOPADHYAY)
LECTURER, SAROJINI NAIDU COLLEGE FOR WOMEN
Birth of C++
 Developed in early 80s at Bell Lab.
 Developed by Bjarne Stroustrup.
 It was based on C.
 The OOP features were added later by Stroustrup.
 It’s a superset of C, ie, any valid C program is also a valid program in
C++ too.
Features of Object Oriented
Programming
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
“
”
C++ is not fully Object
Oriented Language. It’s a
Object based Language.
a) Main function is outside the class
b) Use of Friend function
Class
 Classes are like a blue print or a template or a plan of
what data is to be stored and what functionalities can be
added with those data.
Book
Data :
ISDN CODE
Book Name
Author Name
Publisher’s Name
Price
Functionalities:
Adding new books
Modifying Price
Objects
 The single unit that combine the data and the functions that
operate on that data is called an object.
 Objects are examples of a Class.
 They can represent the real world entities.
 Objects are the only way to access the data.
 E.g. “Gulliver’s Travel” is an example of class Book.
Starting with C++
 Save the file as filename.cpp (.cpp is the extension of the c++ file).
 Compile to get the object file as filename.obj
 Link and make executable to get executable file as filename.exe
compile compile
Linking
File1.cpp File2.cpp
File1.obj File2.obj
File.exe
Introduction to classes and its
features
#include<iostream>
Using namespace std;
Class Example {
private:
int data1;
float data2;
public:
Example() {
data1=0;
data2=0.0;
}
Example(int d1,float d2){
data1=d1;
data2=d2;
}
void getData(int d1,float d2){
data1=d1;
data2=d2;
}
void putData() {
cout<<data1;
cout<<data2;
}
~Example()
{
}
};
Members of a class
Member Data
 data1
 data2
Member Methods
 Example()
 Example(int,int)
 getData(int, int)
 putData()
Constructors
 Is a special member function which is called automatically
whenever an object is create.
 It defines an object, reserves space in memory for the object.
 The name of the constructor must be same as the name of the class
to which it belongs.
 Constructor are of two types mainly – Default and Parameterized.
 Default constructor takes no parameter but parameterized
constructor can take any no of parameters.
 Constructor never returns any value.
Other member methods
 Void getData(int d1,int d2)
 Void putData()
Destructor
 Destructor is a special member method which is called
automatically when an object is being destroyed.
 Destructor generally deallocates the memory allocated to an
object.
 Destructor also has the same name as the class.
 Destructor takes no parameter and returns no value.
The entry point of a program…
 Void main()
 {
 Example ex1,ex2;
 Example ex3(5,5.2);
 int a;
 float b;
 cout << “Enter an number element : :”;
 cin >> a;
 cout << “Enter an fractional element”;
 cin >> b;
 ex2.getData(a,b);
 ex1.putData();
 ex2.putData();
 ex3.putData();
 }
Creation of objects
 Example ex1 - > default constructor is called
 Example ex3(5,5.5) -> parameterized constructor is called
 ex2.getData(a,b) -> member method is called to set the input to
the member data
 ex1.putData() ->member method is called to display the data
members.
 cout is used to print
 Cin is used to get input from the user.
 They both are objects of iostream class

Introduction to Object Oriented Programming through C++.pptx

  • 1.
    Introduction to C++ BYSANHITA GHOSAL (MUKHOPADHYAY) LECTURER, SAROJINI NAIDU COLLEGE FOR WOMEN
  • 2.
    Birth of C++ Developed in early 80s at Bell Lab.  Developed by Bjarne Stroustrup.  It was based on C.  The OOP features were added later by Stroustrup.  It’s a superset of C, ie, any valid C program is also a valid program in C++ too.
  • 3.
    Features of ObjectOriented Programming  Abstraction  Encapsulation  Inheritance  Polymorphism
  • 4.
    “ ” C++ is notfully Object Oriented Language. It’s a Object based Language. a) Main function is outside the class b) Use of Friend function
  • 5.
    Class  Classes arelike a blue print or a template or a plan of what data is to be stored and what functionalities can be added with those data. Book Data : ISDN CODE Book Name Author Name Publisher’s Name Price Functionalities: Adding new books Modifying Price
  • 6.
    Objects  The singleunit that combine the data and the functions that operate on that data is called an object.  Objects are examples of a Class.  They can represent the real world entities.  Objects are the only way to access the data.  E.g. “Gulliver’s Travel” is an example of class Book.
  • 7.
    Starting with C++ Save the file as filename.cpp (.cpp is the extension of the c++ file).  Compile to get the object file as filename.obj  Link and make executable to get executable file as filename.exe compile compile Linking File1.cpp File2.cpp File1.obj File2.obj File.exe
  • 8.
    Introduction to classesand its features #include<iostream> Using namespace std; Class Example { private: int data1; float data2; public: Example() { data1=0; data2=0.0; } Example(int d1,float d2){ data1=d1; data2=d2; } void getData(int d1,float d2){ data1=d1; data2=d2; } void putData() { cout<<data1; cout<<data2; } ~Example() { } };
  • 9.
    Members of aclass Member Data  data1  data2 Member Methods  Example()  Example(int,int)  getData(int, int)  putData()
  • 10.
    Constructors  Is aspecial member function which is called automatically whenever an object is create.  It defines an object, reserves space in memory for the object.  The name of the constructor must be same as the name of the class to which it belongs.  Constructor are of two types mainly – Default and Parameterized.  Default constructor takes no parameter but parameterized constructor can take any no of parameters.  Constructor never returns any value.
  • 11.
    Other member methods Void getData(int d1,int d2)  Void putData()
  • 12.
    Destructor  Destructor isa special member method which is called automatically when an object is being destroyed.  Destructor generally deallocates the memory allocated to an object.  Destructor also has the same name as the class.  Destructor takes no parameter and returns no value.
  • 13.
    The entry pointof a program…  Void main()  {  Example ex1,ex2;  Example ex3(5,5.2);  int a;  float b;  cout << “Enter an number element : :”;  cin >> a;  cout << “Enter an fractional element”;  cin >> b;  ex2.getData(a,b);  ex1.putData();  ex2.putData();  ex3.putData();  }
  • 14.
    Creation of objects Example ex1 - > default constructor is called  Example ex3(5,5.5) -> parameterized constructor is called  ex2.getData(a,b) -> member method is called to set the input to the member data  ex1.putData() ->member method is called to display the data members.
  • 15.
     cout isused to print  Cin is used to get input from the user.  They both are objects of iostream class