cs31 lecture 11

Stdendl stdcout 16 why constructor and destructors are

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: oyed” << std::endl; std::cout } void Car::displayId() { std::cout << uniqueId << std::endl; std::cout 16 Why constructor and destructors are useful x Memory management Some attributes may be dynamically allocated arrays x Perform some class-specific actions when the object is Perform created created Bookkeeping, initialize default values, etc. x Have user-defined creation of objects, taking arguments as Have input input x Debugging You can trace easily when objects are being created/destroyed 17 Example 18 Dynamic arrays in C++ with the STL x Manipulating arrays has been annoying so far! Manipulating x The Standard Template Library provides generic one-dimensional The arrays, whose size is automatically managed arrays, x They are called std::vector x Typical operations: Declare an array of object Car: std::vector<Car> myvector; Adding an element to it, at the end: myvector.push_back(Car()); Accessing it: myvector[0].displayId(); // mycar.size() is available 19 Inheritance x Probably the most powerful concept in OOP x Basic idea: a class can inherit from another class It inherits all its attributes and methods It can of course have new methods/attributes It can redefine attributes and methods x Some terminology: The parent class: superclass The current class (which inherits from some class): the subclass 20 Some syntax x Class A inherits from class B struct A : B { }; x Class A inherits from classes B and C struct A : B, C { }; x Call a method foo() from the parent class B from class A B::foo() foo() if only one parent defines foo() (no ambiguity) 21 Example 22...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online