View
216
Download
0
Category
Preview:
Citation preview
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 contain class, function, and global
variable declarations/definitions main() function serves as entry point for the
executable
Header Files (.h)
Contains class declarations Prototypes for functions outside of classes Other declarations
Source Files (.cpp)
Function/method definitions Directives (e.g., #include) Variables and initialization (global and static
variables)
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];
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
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
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; }
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
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 ) …
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
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
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
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
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!
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
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?
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).
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
Recommended