Programming Languages and Paradigms Programming C Classes 6152005

  • Slides: 15
Download presentation
Programming Languages and Paradigms Programming C++ Classes 6/15/2005

Programming Languages and Paradigms Programming C++ Classes 6/15/2005

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

Program Structure n n n C++ Program: collection of files Files contain class, function, and global variable declarations/definitions main() function still the entry point just like in C 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 2

Header Files (. h) n n n Contains class declarations Prototypes for functions outside

Header Files (. h) n n n Contains class declarations Prototypes for functions outside of classes Others 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 3

Source Files (. cpp) n n n Function/method definitions Directives (e. g. , #include,

Source Files (. cpp) n n n Function/method definitions Directives (e. g. , #include, #define) Variables and initialization (global/static variables) 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 4

Variables in C++ n Regular variables n n Pointers n n n int *p;

Variables in C++ n Regular variables n n Pointers n n n int *p; p = &x; Button *q; q = new Button(); References/Aliases n n int x; x = 5; Button b; int &r = x; // initialization required Arrays n 6/15/2005 int num[20]; int *a; a = new int[size]; Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 5

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

Class Declaration n class A { members … }; Members (fields and methods) grouped by public, private or protected regions Fields can be regular variables, arrays, pointers, or references n n 6/15/2005 static (class-level) fields are permitted Constructor/initialization is more involved (than in Java) Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 6

Field Initialization in C++ n n Initialization cannot be performed during field declaration For

Field Initialization in C++ n n 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: n 6/15/2005 classname( type param ): fieldname( param ) … Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 7

Regular Initialization class Bank. Account object { private: holder int balance; Person *holder; public:

Regular Initialization class Bank. Account object { private: holder int balance; Person *holder; public: Bank. Account( int b, Person *p ) { balance = b; holder = p; } }; … Person john; Bank. Account b( 1000, &john ); 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. Person object 8

Comment on Object Fields Creates a Person object for every Bank. Account object; Probably

Comment on Object Fields Creates a Person object for every Bank. Account object; Probably not the intention Bank. Account object class Bank. Account { holder private: Person object int balance; Person holder; public: Copy constructor, Bank. Account( int b, Person p ) then assignment, { balance = b; holder = p; } is invoked }; … Person john; Bank. Account b( 1000, john ); 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 9

Initializing References class Bank. Account { private: int balance; Person& holder; public: Bank. Account(

Initializing References class Bank. Account { private: int balance; Person& holder; public: Bank. Account( int b, Person& p ) : holder( p ) { balance = b; } }; … Person john; Bank. Account b( 1000, john ); 6/15/2005 holder is an alias to an external object; Existence of the object is enforced Bank. Account object Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. holder john Person object 10

Comparison n Use regular variables for tight object composition n n Use pointers for

Comparison n Use regular variables for tight object composition n n Use pointers for associations n n contained object is created during object construction of the containing class Associated object is created externally and may be updated (e. g. , void change. Account. Holder(Person *p)…) Use references for more permanent associations and to ensure the existence of the associated object upon construction 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 11

Back to Object Field Example class Car { private: Constructor, Engine eng; copy constructor,

Back to Object Field Example class Car { private: Constructor, Engine eng; copy constructor, public: then assignment, is invoked; Car( Engine e ) 3 operations! { eng = e; } }; … Engine newengine; Car c( newengine ); 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 12

Better to use references even with object fields class Car Only the copy constructor

Better to use references even with object fields class Car Only the copy constructor is invoked; { eng is built from e, which is private: an alias for newengine Engine eng; public: Car( Engine& e ): eng(e) {} }; … Engine newengine; Car c( newengine ); 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 13

Inheritance and Constructors class Employee { … public: Employee() { } // this is

Inheritance and Constructors class Employee { … public: Employee() { } // this is called Employee( string& s ) { } // not this }; class Manager: public Employee { Manager’s constructor … implicitly calls the default public: constructor of employee; Manager( string& s ) { } How do we call a specific Employee constructor? }; 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 14

Inheritance and the Special Constructor Syntax class Employee { … Use the special syntax

Inheritance and the Special Constructor Syntax class Employee { … Use the special syntax to call a public: particular superclass Employee() { } constructor (analogous to super() in Java). Employee( string& s ) { } }; class Manager: public Employee { … public: Manager( string& s ): Employee( s ) { } }; 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 15