1 / 19

OOP in C++

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.

hallbeth
Download Presentation

OOP in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 ); BankAccountobject holder Person object

  12. Comment on Object Fields Creates a Person object for every BankAccount object; Probably not the intention BankAccountobject holder class BankAccount { private: int balance; Person holder; public: BankAccount( int b, Person p ) { balance = b; holder = p; } }; … Person john; BankAccount b( 1000, john ); Person object Copy constructor, then assignment, is invoked

  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 BankAccountobject holder john Person object

  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 theSpecial 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

More Related