Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages and Paradigms Programming C++ Classes.

Similar presentations


Presentation on theme: "Programming Languages and Paradigms Programming C++ Classes."— Presentation transcript:

1 Programming Languages and Paradigms Programming C++ Classes

2 6/15/2005 Copyright 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

3 6/15/2005 Copyright 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

4 6/15/2005 Copyright 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)

5 6/15/2005 Copyright 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];

6 6/15/2005 Copyright 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)

7 6/15/2005 Copyright 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 ) …

8 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 8 Regular Initialization class 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 Person object

9 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 9 Comment on Object Fields class 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 object for every BankAccount object; Probably not the intention Copy constructor, then assignment, is invoked BankAccount object holder Person object

10 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 10 Initializing References class 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 to an external object; Existence of the object is enforced BankAccount object holder Person object john

11 6/15/2005 Copyright 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

12 6/15/2005 Copyright 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!

13 6/15/2005 Copyright 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

14 6/15/2005 Copyright 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?

15 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L7: C++ Slide 15 Inheritance and the Special 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).


Download ppt "Programming Languages and Paradigms Programming C++ Classes."

Similar presentations


Ads by Google