Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.

Similar presentations


Presentation on theme: "OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files."— Presentation transcript:

1 OOP in C++ CS 124

2 Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files contain class, function, and global variable declarations/definitions main() function serves as entry point for the executable

3 Header Files (.h) Contains class declarations Prototypes for functions outside of classes Other declarations

4 Source Files (.cpp) Function/method definitions Directives (e.g., #include) Variables and initialization (global and static variables)

5 Variables in C++ Regular variables int x; x = 5; BankAccount b; Pointers int *p; p = & x; BankAccount *q; q = new BankAccount; References/Aliases int & r = x; // initialization required Arrays int num[20]; int *a; a = new int[size]; BankAccount *many; many = new BankAccount[10];

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 Methods often defined outside of class declaration If defined inside declaration, it will be an inline method Convention: class declaration is in a.h file, method definitions are in a corresponding.cpp file

7 Constructors and destructors Constructor: special “method” used for object initialization Signature: same as class name, no return type May have several forms if overloaded (constructors with or without parameters) Called in many situations: variable declaration, new, array creation, … Destructor: special “method” used for object destruction Called when object variables go out of scope or during delete

8 C++ Destructor Special method whose signature is a ~ followed by the name of the class e.g., ~SomeClass(); Particularly if the class contains pointers and the constructor contains calls to new, a destructor needs to be defined e.g., SomeClass() { A = new int[20]; } ~SomeClass() { delete [] A; }

9 C++ Control Over Copy and Assignment In C++, the semantics of “a = b” (assignment) can be specified by defining the assignment operator In C++, there is a copy constructor specifies what happens during object copying, e.g., when function parameters are passed There is more low-level control shallow copy vs deep copy Both a copy constructor and an assignment operator should be defined if there are dynamically allocated portions in constructor

10 Field Initialization in C++ Fields: attributes/variables of a class 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 ) …

11 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

12 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

13 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

14 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

15 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!

16 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

17 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?

18 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).

19 Other important C++ features Virtual functions (for dynamic binding) Multiple inheritance (how to disambiguate between conflicting methods) Operator overloading (operators as methods) Templates (generics) Namespaces (managing identifiers) * See sample code on website


Download ppt "OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files."

Similar presentations


Ads by Google