23 CPP Inheritance Cont
23 CPP Inheritance Cont
Inheritance
Programming Concepts and
Tools
1
Administrivia
HW 3 posted Friday -> Extra credit due date Wednesday Nov 25th @ 9pm
End of quarter due date Wednesday December 16th @ 9pm
If you want to use the base-class code, specify the base class when making a method call
- class::method(…)
- Like Java “super” but C++ doesn’t have “super” because of multiple inheritance
polymorphism is the ability to access different objects through the same interface
deleted constructors
- C++ automatically generates a “copy constructor” for your class if you do not provide one, however sometimes you
want to prevent copies. (EX: copying bank account objects). Instead declare a copy constructor in the header file and
set the constructor “= delete;” which means we delete anything created and prevent it from being used anywhere else
Down Casting
- C pointer-casts: unchecked; be careful
- Java: checkedl; may raise ClassCastException
- New: C++ has “all the above” (ie several different kinds of casts)
- if you use single-inheritance and know what you are doing, the C-style casts (same pointer, assume more about what is pointed to)V should work
fine for down casts
A derived class:
Inherits the behavior and state (specification) of the base class
Overrides some of the base class’ member functions (opt.)
Extends the base class with new member functions, variables (opt.)
class A {
public:
A() { cout << "a()" << endl; }
~A() { cout << "~a" << endl; }
void m1() { cout << "a1" << endl; }
void m2() { cout << "a2" << endl; }
};
A class containing only pure virtual methods is the same as a Java interface
- Pure type specification without implementations
a non-virtual method call is resolved using the compile-time type of the receiver expression
a virtual method call is resolved using the run-time class of the receiver object (what the expression evaluates to)
- Aka: dynamic dispatch
A method-call is virtual if the method called is market virtual or overrides a virtual method
- so “one virtual” somewhere up in the base-class chain is enough, but it’s better style to be more explicit and repeat “virtual”
A member function invoked on an object should be the most-derived function accessible to the
object’s visible type
- Can determine what to invoke from the object itself
Example:
- void PrintStock(Stock* s) { s->Print(); }
Calls the appropriate Print() without knowing the actual type of *s, other than it is some sort of Stock
Functions just like Java
Unlike Java: Prefix the member function declaration with the virtual keyword
- Derived/child functions don’t need to repeat virtual, but was traditionally good style to do so
- This is how method calls work in Java (no virtual keyword needed)
- You almost always want functions to be virtual
class E : public C {
};
CSE 374 AU 20 - KASEY CHAMPION 18
Questions
Point foo() {
Point y; // default ctor
return y; // copy ctor? optimized?
}
Namespace definition:
- namespace name {
// declarations go here
}
- Doesn’t end with a semi-colon and doesn’t add to the indentation of its contents
- Creates a new namespace name if it did not exist, otherwise adds to the existing namespace (!)
- This means that components (e.g. classes, functions) of a namespace can be defined in multiple source files
Namespaces vs classes
- They seems somewhat similar, but classes are not namespaces:
- There are no instances/objects of a namespace; a namespace is just a group of logically-related things (classes, functions,
etc.)
- To access a member of a namespace, you must use the fully qualified name (i.e. nsp_name::member)
- Unless you are using that namespace
- You only used the fully qualified name of a class member when you are defining it outside of the scope of the class definition
CSE 374 AU 20 - KASEY CHAMPION 22
Const
C++ introduces the “const” keyword which declares a value that cannot change
const int CURRENT_YEAR = 2020;