21
OOP in C++ By: Saif Bashar Introduction to Classes 2017 www.saifacademy.com Updated on 11-12

Lecture 10 classes

Embed Size (px)

Citation preview

Page 1: Lecture 10 classes

OOP in C++

By: Saif Bashar

Introduction to Classes

2017

www.saifacademy.com

Updated on 11-12

Page 2: Lecture 10 classes

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

Page 3: Lecture 10 classes

Object Oriented Programming

Objects have some attributes

Objects have some functionalityObject

Attributes

Methods

Page 4: Lecture 10 classes

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

Page 5: Lecture 10 classes

Pillars of OOP

Polymorphism

Inheritance

OOPEncapsu

lation

Page 6: Lecture 10 classes

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

Page 7: Lecture 10 classes

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)

Page 8: Lecture 10 classes

Class Definitionclass className {

private:

data1;

data2;

data3;

public:

function1(){}

function2(){}

function3(){}

protected:

};

Data members

Member functions (Methods)

Page 9: Lecture 10 classes

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

Page 10: Lecture 10 classes

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

Page 11: Lecture 10 classes

OOP

Encapsulation

The details of the class implementation are hidden and contained inside the class

It is also called data hiding

Page 12: Lecture 10 classes

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

Page 13: Lecture 10 classes

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

Page 14: Lecture 10 classes

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

Page 15: Lecture 10 classes

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

Page 16: Lecture 10 classes

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

Page 17: Lecture 10 classes

Building a Complete Class

Definition

Data Members

Member Functions

Constructor

Destructor

Object

Class

Page 18: Lecture 10 classes

OOP

File Separation

Classes usually separated into two files:

- The header file (contains the class definition)

- The Implementation file (contains the class implementation)

Page 19: Lecture 10 classes

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

Page 20: Lecture 10 classes

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

Page 21: Lecture 10 classes

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