comsw 1003-1 introduction to computer programming inmmerler/coms1003-1/lec24.pdf · c object...

37
C C COMSW 1003-1 Introduction to Computer Programming in C 1 Spring 2011 Instructor: Michele Merler http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html Lecture 24

Upload: others

Post on 13-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

COMSW 1003-1

Introduction to Computer Programming in C

1

Spring 2011

Instructor: Michele Merler

http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html

Lecture 24

Page 2: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Announcements

2

• HW 5 due on Wednesday

• 2 extra days for who submitted on time HW4 (deadline Friday, April 29th 10am)

Page 3: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

C++

3

• Younger brother of C

• Appeared in 1983

• Object Oriented

• Can be compiled with gcc, usually g++ is used

Page 4: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Review - C++

4

• Main factors differentiating C++ from C:

– Slightly different syntax, contains type bool

– Functions overloading

– Object oriented

Page 5: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Review - Hello World++

5

• File extension .cpp ( C++ uses also .h)

• I/O : <iostream>, <fstream>

cin >> , cout <<, endl

(i/o)fstream()

• Automatic casting when reading variables

• Variables can be declared anywhere

for( int i=0; i<10; i++ )

• bool type

bool x;x = true || false;

Hello.cpp

Page 6: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Review - Dynamic Memory Allocation

6

• New (equivalent of malloc() / calloc() )

• Delete (equivalent of free() )

• No realloc() !

float *arr = new float[7];

float *arr = (float *) malloc( 7 * sizeof(float) );C

delete [] arr; free( arr );C

dynmem.cpp

Page 7: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Review C++ Standard Template Library(STL)

7

• Vector– Array, at declaration must specify type– Assignment between whole arrays– Functions to determine array size, swap elements, etc.

Provides special C++ “types” (class templates).

Anything from the standard library must be preceded by the std:: prefixAlternatively, we can put using namespace std at the beginning

templates.cpp

• List• Queue• Stack

. . .

Dynamic memory allocation managed by C++ !

http://www.cplusplus.com/reference/stl/

Page 8: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Review Strings

8

• Enhanced functionalities wrt C string

• Perhaps the most interesting is the use of + to concatenate strings

• find_fist_of,() find_last_of(), substr(), etc.

• Dynamic memory allocation managed by C++

templates.cpp

http://www.cplusplus.com/reference/string/string/

Page 9: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Functions Overloading

9

• Use function with same name in different fashions

• Behavior of function depends on:– The number of arguments– The data type of arguments– The order of appearance of arguments

• C++ automatically determines which implementation of the function to use given arguments

funOverload.cpp

Page 10: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Object Oriented Programming

10

Page 11: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC 11

Programming Paradigms

• Unstructured Programming

• Procedural Programming

• Modular Programming

• Object Oriented Programming

11

Page 12: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Unstructured Programming

12

• One single file

• Only one block of code: the main() function

• Data manipulated sequentially inside main()

mainFileProgram

Data

Main()

Page 13: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Procedural Programming

13

• One single file

• Multiple blocks of code grouped in functions (or procedures)

• Data manipulated inside functions ()

mainFileProgram

Data

Main( )

Function2( )

Function1( )

Function3( )

Page 14: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Modular Programming

14

#include “Module1.h”

mainFile.cModule1

ModuleN

Program

Data

Data

Data

Function( )

Function( )

Function( )

main( )

• Multiple files

• Functions of similar logical goal grouped into modules

• Different data manipulated inside functions in modules

Page 15: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Object Oriented Programming

15

• Based on objects interacting with each other

• Objects exchange messages, but maintain their state and data

• Usually associated also with modular programming

Program

Data

DataData

Page 16: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC 16

Object oriented programming

• Classes

• Objects

• Inheritance

• Polymorphism

16

Page 17: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Objects• An object is an entity, for example a car, a building, a phone…

• An object can be defined by its features (attributes) or by its behavior (functions it provides)

• For example a car:– has wheels, seats, an engine– has make, model, year– can run at 100mph, transport people

• In programming, we can think of an object as a struct variable, but with enhanced capabilities

17

Page 18: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Classes• Defining an object through features and functionalities

it too generic, we are defining a class of objects

• An object is a single instance of a class

• For example my Ferrari F40, with 120K miles and a scratch on the side is an object, and belongs to the class of cars

18

Class carCar 2Car 1

Car 10 Car 3

Car n

Page 19: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Classes

• We can think of classes and objects in terms of familiar C/C++ types

• A class is an enhanced struct– class car

• An object is a variable of type class– car c1

19

Page 20: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Attributes and Methods

• We can think of a class as a struct with enhanced capabilities. A class has– Attributes ( variables, like the fields in struct )

– Methods ( member functions)

20

• Make• Model• Year• Color

Class car

• printAttributes()• getYear()• setYear()

Attributes

Methods

Page 21: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Attributes and Methods

• We can think of a class as a struct with enhanced capabilities. A class has– Attributes ( variables, like the fields in struct )

– Methods ( member functions)

21

• Make• Model• Year• Color

Class car

• printAttributes()• getYear ()• setYear()

Attributes

Methods

• toyota• corolla• 1992• blue

car c1

• printAttributes()• getYear ()• setYear()

Page 22: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

class car{

public:car(int, string, string, string);

~car(void){};

int getYear(void) const { return year;}

int setYear(int);

void printAttributes(void);

protected:int year; string make; string model;string color;

};

Class declaration• There are two sections in a class

– Public : accessible from anybody

– Private/protected : accessible only from class methods (and sub-classes or friend classes)

22

• Make• Model• Year• Color

Class car

• printAttributes()• currentSpeed()

Attributes

Methods

car.h

Page 23: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Constructor

23

• Constructor is the function that initializes (creates) an object of a class

• It is invoked when an object of that class is created

• Has the same name of the class

car(void){};

car(int y){ year = y; };

car( int, string, string, string );

car::car( int y, string mk, string mo, string c){

year = y;make = mk;model = mo;color = c;

}

car.h

car.cpp

car c1( 1993, "Toyota", "Corolla", "Green" );

declaration

main.cpp

declaration +

definition

definition

object

Page 24: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Destructor

24

• Opposite of constructor

• Function that is invoked when an object is destroyed

• Can be useful for dynamic memory de-allocation

~car(void){};

car.h

In this case it does not do anything

Page 25: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Methods and Operators Definition

25

int car::setColor( string s ){color = s;return 0;

}

car car::operator=( car C ) {

car tmp;tmp.year = C.getYear();tmp.make = C.getMake();tmp.model = C.getModel();tmp.color = C.getColor();

return tmp;}

car.cpp

We must specify to which class does the function (method) belong

The operator assignment creates a new object tmpand returns it

Page 26: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Inheritance• There can exist subclasses, or derived classes

of a class

• Example: class car can have subclasses– race car– SUV– city car

• All derived classes inherit the attributes and methods of the parent class

• They also add their new attributes and/or methods 26

Page 27: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC 27

• Make• Model• Year• Color

• printAttributes()• getYear()

• Make• Model• Year• Color• Pilot

• printAttributes()• getYear()• numRaces()

• Make• Model• Year• Color• Shaded windows

• printAttributes()• getYear()

• Make• Model• Year• Color

• printAttributes()• getYear()• isParked()

car

Race car SUV City car

Page 28: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Inheritance

• Inheritance can be

– Single : a class derives from only one other class

– Multiple: a class derives from multiple classes

28

vehicle

single multiple

car motorbike

Electronic device

Communication tool

phone

Page 29: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Inheritance

29

class SUV : public car{

public:

SUV(void) : car() { shadedWindows = false; }

~SUV(void) {};

bool windowsShaded(void) const {return shadedWindows;};

void printAttributes(void);

private:

bool shadedWindows;

};

car.h

Page 30: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Inheritance

30

class SUV : public car{

public:

SUV(void) : car() { shadedWindows = false; }

~SUV(void) {};

bool windowsShaded(void) const {return shadedWindows;};

void printAttributes(void);

private:

bool shadedWindows;

};

car.h

Inherits from class car

Calls the constructor of class car

Page 31: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Inheritance

31

class SUV : public car{

public:

SUV(void) : car() { shadedWindows = false; }

~SUV(void) {};

bool windowsShaded(void) const {return shadedWindows;};

void printAttributes(void);

private:

bool shadedWindows;

};

car.h

Attribute and methods which are peculiar of this subclass The superclass car does not have them

Page 32: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Polymorphism• Different subclasses can have different implementations of a

function declared in their parent class

• Example: printAttributes ()

32

void car::printAttributes(void){

cout<< "car attributes" <<endl;cout<< "Year:" << year <<endl;cout<< "Make:" << make <<endl;cout<< "Model:" << model <<endl;cout<< "Color:" << color <<endl;

}

void SUV::printAttributes(void){

cout<< "car attributes" <<endl;cout<< "Year:" << year <<endl;cout<< "Make:" << make <<endl;cout<< "Model:" << model <<endl;cout<< "Color:" << color <<endl;cout<< "Windows shaded : " << shadedWindows <<endl;

}

car.cpp

Page 33: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Polymorphism• Different subclasses can have different implementations of a

function declared in their parent class

• Example: printAttributes ()

33

void car::printAttributes(void){

cout<< "car attributes" <<endl;cout<< "Year:" << year <<endl;cout<< "Make:" << make <<endl;cout<< "Model:" << model <<endl;cout<< "Color:" << color <<endl;

}

void cityCar::printAttributes(void){

cout<< "car attributes" <<endl;cout<< "Year:" << year <<endl;cout<< "Make:" << make <<endl;cout<< "Model:" << model <<endl;cout<< "Color:" << color <<endl;cout << "Parked : " << isParked() << endl;

}

car.cpp

Page 34: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Polymorphism• Different subclasses can have different implementations of a

function declared in their parent class

• Example: printAttributes ()

34

void car::printAttributes(void){

cout<< "car attributes" <<endl;cout<< "Year:" << year <<endl;cout<< "Make:" << make <<endl;cout<< "Model:" << model <<endl;cout<< "Color:" << color <<endl;

}

void raceCar::printAttributes(void){

cout<< "car attributes" <<endl;cout<< "Year:" << year <<endl;cout<< "Make:" << make <<endl;cout<< "Model:" << model <<endl;cout<< "Color:" << color <<endl;cout << "Pilot : " << pilot << endl;

}

car.cpp

Page 35: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Templates

35

• Classes that work with any type

• vector is an example of template

template <class T> class Matrix {

private:int r;int c;vector< vector<T> > data;

public:Matrix(int rows, int columns); ~Matrix() {}

int rows() const {return r;} int columns() const {return c;} T& operator () (int row, int col); T& operator () (int row, int col) const;

};

matrix.h

Page 36: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Templates

36

• Classes that work with any type

• vector is an example of template

template <class T> class Matrix {

private:int r;int c;vector< vector<T> > data;

public:Matrix(int rows, int columns); ~Matrix() {}

int rows() const {return r;} int columns() const {return c;} T& operator () (int row, int col); T& operator () (int row, int col) const;

};

matrix.h

Page 37: COMSW 1003-1 Introduction to Computer Programming inmmerler/coms1003-1/Lec24.pdf · C Object Oriented Programming 15 •Based on objectsinteracting with each other •Objects exchange

CC

Templates

37

• Once I have declared and defined a template, I can use it with any type I want

Matrix<float> M(2,3);

float maxVal;M.findMax( maxVal );

main.cpp