Upload
saif-bashar
View
50
Download
1
Embed Size (px)
Citation preview
OOP in C++
By: Saif Bashar
Introduction to Classes
2017
www.saifacademy.com
Updated on 11-12
Object Oriented Programming
› New way of looking at things
› Large programs became interacting objects
› Everything is an object that belongs to a class
› Classes developed and tested independently
› Separating the implementation from the interface
Object Oriented Programming
Objects have some attributes
Objects have some functionalityObject
Attributes
Methods
Object Oriented Programming
Objects have some attributes
Objects have some functionality
Class: Player
Object: CR7
Object: Messi
NameNationalityPositionRun ()Skills ()Shoot ()Pass ()Dribble ()Header ()
RonaldoPortugalForward908095808599
MessiArgentinaForward908593909080
Pillars of OOP
Polymorphism
Inheritance
OOPEncapsu
lation
C vs C++
int char float double bool for if else switch case default while do break continue struct auto const enum
extern goto long short signed unsigned sizeof
return union static register typedef void
volatile
new
private
public
protected
template
operator
class
delete
asmfriend
catch
try
inline
thisthrow
virtual
C has 33 keyword
C++ has 49 keyword
New Keywords added in new versions
Object Oriented Programming
› Classes are user defined types - UDT
› Objects are instances of a class
› Objects contains data and functions
› Data are called (data members)
› Functions are called (member functions) or (methods)
Class Definitionclass className {
private:
data1;
data2;
data3;
public:
function1(){}
function2(){}
function3(){}
protected:
};
Data members
Member functions (Methods)
Class Rectangle
class rectangle {
private:
int width;
int length;
public:
int area(){
return (width *length);
}
void setValues(int x, int y){
width = x;
length = y;
}
};
int main () {
rectangle r1;
r1.setValues(3,5);
cout<<r1.area();
rectangle r2;
r2.setValues(4,6);
cout<<r2.area();
return 0;
}
Example
Notes on rectangle class
› Data members should remain private
› Data members can be accessed only by class methods
› We specify some methods to be public
› Public methods can be called from outside the class
OOP
Encapsulation
The details of the class implementation are hidden and contained inside the class
It is also called data hiding
Class Constructor
› Initialize the class data members
› Reserve a space in memory for the object
› Has the same name of the class
› Has no return type
› Can be overloaded
› Called automatically when we declare an object
Class Rectangle
class rectangle {
private:
int width;
int length;
public:
int area(){
return (width *length);
}
rectangle(int x, int y){
width = x;
length = y;
}
};
int main () {
rectangle r1 (3,5);
cout<<r1.area();
rectangle r2(4,6);
cout<<r2.area();
return 0;
}
Example
Class Destructor
› Destroy the object
› Free the memory occupied by the object
› Has the same name of the class preceded with (~)
› Has no return type
› Has no arguments
› Can not be overloaded
› Called automatically when the object’s scope ends
Class Rectangle
class rectangle {
private:
int width;
int length;
public:
int area(){
return (width * length);}
rectangle(int x, int y){
width = x;
length = y;}
~rectangle(){
cout<<“Object is destroyed”;}
};
int main () {
rectangle r1 (3,5);
cout<<r1.area();
rectangle r2(4,6);
cout<<r2.area();
return 0;
}
Example
Class Rectangle
class rectangle {
private:
int *width; int *length;
public:
int area(){
return (*width * *length);}
rectangle(int x, int y){
width = new int;
length = new int;
*width = x;
*length = y;}
~rectangle(){
delete width; delete length; }
};
int main () {
rectangle r1 (3,5);
cout<<r1.area();
rectangle r2(4,6);
cout<<r2.area();
return 0;
}
Example
Building a Complete Class
Definition
Data Members
Member Functions
Constructor
Destructor
Object
Class
OOP
File Separation
Classes usually separated into two files:
- The header file (contains the class definition)
- The Implementation file (contains the class implementation)
Scope Resolution Operator
› We can use the scope resolution operator
› To define the methods outside the class
› The method prototype must be included in the class definition
Class Rectangle
class rectangle {
private:
int width;
int length;
public:
int area();
rectangle(int, int);
~rectangle();
};
int rectangle::area(){
return (width * length);}
rectangle::rectangle(int x,
int y){ width = x;
length = y;}
rectangle::~rectangle(){
cout<<“Object is
destroyed”;}
Example
class rectangle {
private:
int width;
int length;
public:
int area();
rectangle(int, int);
~rectangle();
};
#include ”rectangle.h”
int rectangle::area(){
return (width * length);}
rectangle::rectangle(int x,
int y){ width = x;
length = y;}
rectangle::~rectangle(){
cout<<“Object is
destroyed”;}
rectangle.h rectangle.cpp