19
OOP in C++ CS 124

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

Embed Size (px)

Citation preview

Page 1: 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

OOP in C++

CS 124

Page 2: 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

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

Page 3: 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

Header Files (.h)

Contains class declarations Prototypes for functions outside of classes Other declarations

Page 4: 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

Source Files (.cpp)

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

variables)

Page 5: 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

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];

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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; }

Page 9: 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

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

Page 10: 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

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 ) …

Page 11: 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

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 Personobject

Page 12: 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

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 objectfor every BankAccount object;Probably not the intention

Copy constructor,then assignment,is invoked

BankAccount

object

holderPerson

object

Page 13: 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

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 toan external object;Existence of the objectis enforced

BankAccount

object

holder Person

object

john

Page 14: 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

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

Back to Object Field Exampleclass 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 16: 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

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

Inheritance and Constructorsclass 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 18: 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

Inheritance and theSpecial Constructor Syntaxclass 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).

Page 19: 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

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