programming languages and paradigms programming c++ classes

15
Programming Languages and Paradigms Programming C++ Classes

Upload: bertram-hensley

Post on 03-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Programming Languages and Paradigms Programming C++ Classes

Programming Languagesand Paradigms

Programming C++ Classes

Page 2: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 2

Program Structure

C++ Program: collection of files Files contain class, function, and

global variable declarations/definitions main() function still the entry point

just like in C

Page 3: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 3

Header Files (.h)

Contains class declarations Prototypes for functions outside of

classes Others

Page 4: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 4

Source Files (.cpp)

Function/method definitions Directives (e.g., #include, #define) Variables and initialization

(global/static variables)

Page 5: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 5

Variables in C++ Regular variables

int x; x = 5; Button b; Pointers

int *p; p = &x; Button *q; q = new Button();

References/Aliases int &r = x; // initialization required

Arrays int num[20]; int *a; a = new int[size];

Page 6: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 6

Class Declaration class A { members … }; Members (fields and methods)

grouped by public, private or protected regions

Fields can be regular variables, arrays, pointers, or references static (class-level) fields are permitted Constructor/initialization is more involved

(than in Java)

Page 7: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 7

Field Initialization in C++ Initialization cannot be performed during

field declaration For non-reference variables, initialization

can be carried out in the constructor body Static fields need to be initialized separately

(outside the class declaration) as a global variable

For, reference variables, use special constructor syntax: classname( type param ): fieldname( param ) …

Page 8: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 8

Regular Initializationclass BankAccount{ private: int balance; Person *holder; public: BankAccount( int b, Person *p ) { balance = b; holder = p; } };…Person john; BankAccount b( 1000, &john );

BankAccount

object

holder Personobject

Page 9: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++

Slide 9

Comment on Object Fieldsclass BankAccount{ private: int balance; Person holder; public: BankAccount( int b, Person p ) { balance = b; holder = p; } };…Person john; BankAccount b( 1000, john );

Creates a Person objectfor every BankAccount object;Probably not the intention

Copy constructor,then assignment,is invoked

BankAccount

object

holderPerson

object

Page 10: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++Slide 10

Initializing Referencesclass BankAccount{ private: int balance; Person& holder; public: BankAccount( int b, Person& p )

:holder( p ) { balance = b; } };…Person john; BankAccount b( 1000, john );

holder is an alias toan external object;Existence of the objectis enforced

BankAccount

object

holder Person

object

john

Page 11: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++Slide 11

Comparison Use regular variables for tight object composition contained object is created during object

construction of the containing class Use pointers for associations

Associated object is created externally and may be updated (e.g., void changeAccountHolder(Person *p)…)

Use references for more permanent associations and to ensure the existence of the associated object upon construction

Page 12: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++Slide 12

Back to Object Field Example

class Car{ private: Engine eng; public: Car( Engine e ) { eng = e; } };…Engine newengine; Car c( newengine );

Constructor,copy constructor,then assignment,is invoked;3 operations!

Page 13: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++Slide 13

Better to use references even with object fields

class Car{ private: Engine eng; public: Car( Engine& e ): eng(e) {} };…Engine newengine; Car c( newengine );

Only the copy constructor is invoked;eng is built from e, which is an alias for newengine

Page 14: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++Slide 14

Inheritance and Constructors

class Employee{…public: Employee() { } // this is called Employee( string& s ) { } // not this};class Manager: public Employee{…public: Manager( string& s ) { }};

Manager’s constructor implicitly calls the default constructor of employee;How do we call a specific Employee constructor?

Page 15: Programming Languages and Paradigms Programming C++ Classes

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L7: C++Slide 15

Inheritance and theSpecial Constructor Syntax

class Employee{…public: Employee() { } Employee( string& s ) { }};class Manager: public Employee{…public: Manager( string& s ): Employee( s ) { }};

Use the special syntax to call a particular superclass constructor(analogous to super() in Java).