CS107 Study Guide

Unit 4: Object-Oriented Programming

4a. Define and use constructors and deconstructors

  • What is a constructor?
  • What is a destructor?
  • How are constructors and destructors executed?

A constructor is a method that executes automatically when an object from a class is instantiated within a program. A constructor must have the same name as the class, this is how the compiler identifies constructor methods within a class definition. Constructor methods do not have a return type, their sole purpose is to instantiate class objects. The most basic type of constructor is the default constructor which takes no input arguments. If no constructor is included in the class definition, a default constructor will always run in order to instantiate an object. User-defined constructors can have input values if needed and are used to initialize an object's data attributes at the time an object is instantiated. Copy constructors create a new object which is a copy of an existing object, a very convenient feature when it comes to cloning objects.

Constructors can be overloaded allowing a class definition to have several constructors if desired. C++ distinguishes between different constructors based upon the order and type of input variable (i.e. according to the rules of function overloading which should be reviewed). This allows for flexibility in the use of a class.

Destructors are automatically executed when an object is destroyed. For example, a destructor will run when an object is deleted (e.g. using the del command) or when a process involving an object terminates (e.g. when main ends). Similar to a constructor, a destructor has the same name as the class; but, additionally, it is preceded by the symbol ~ in order to distinguish it from a constructor. Typical kinds of tasks for destructors involve clean-up operations such as memory deallocation.

To review, see Classes and Objects and Function Overloading.


4b. Create overloading operators

  • What is operator overloading?
  • Which operators can/cannot be overloaded?
  • What is the syntax for overloading?

Operator overloading allows the programmer to create a different meaning for a C++ operator depending upon how it is used. A list of operators that can be overloaded should be reviewed, but, as a general rule, arithmetic, relational and logical operators can be overloaded. In addition, increment/decrement operations such ++ or -- can be overloaded. On the other hand, higher level operations such as :: (scope resolution) and . (object reference) cannot be overloaded. It should be pointed out that operator overloading does not imply the creation of new operators, but, rather, it refers to a different use of an existing operator.

Operator overloading syntax should be reviewed as there is much detail surrounding this subject. At the most basic level, reference must be made to the class name so that objects from the class can apply the overloaded operator. The operator keyword followed by the overloaded operator is also fundamental to overloading syntax. As an example of a case involving a nonmember function for a class named Examp to overload the + operator, the definition would follow the pattern:

Examp operator+(input arguments){ C++ code for implementation}

Other factors such as access to private or public members will dictate syntactical additions necessary for complete implementation across the class definition.

To review, see Operator Overloading.


4c. Define and use the keyword "this" and use the static members appropriately

  • What is the keyword this used for?
  • What is the syntax for using this?
  • What is a static attribute?

The keyword this is used to refer to the current object of a class. It can be used to refer to class data members whose name conflicts with an input variable name used in a member function. Syntax for using this involves the -> operator. Its typical use might look something like:

this->a =a;

where the variable a has been input to a member function and a has also been used as the name of a data member within the associated class. The -> notation arose to make a clear delineation between a pointer to a structure and a pointer to a data member within a structure. It naturally carries over to refer to class objects.

Static attributes refer to data members whose value will persist beyond the instantiation of any object. In this way, instantiated objects from the same class will all see the same value for the static member. This first time an object is instantiated, the static data member will be set by the class definition or within an associated member definition declaration. After that, if another object is instantiated, the data member can retain its value (hence, the name static) from any previous instantiation.

To review, see C++ Programming/Classes/this and Static Attributes.


4d. Design and appropriately use friend functions and classes

  • Why is the friend keyword useful?
  • What is a friend function?
  • What is a friend class?

Private members of a class are inaccessible and cannot be modified (except through accessors and mutators). Aside from class children, protected members are also inaccessible. The friend keyword is used to enable the sharing of class information that might otherwise be inaccessible (such as private or protected members).

Within a class definition, friend functions are declared using the friend keyword. In this way, a class can make its data accessible to a specific function. The function is said to be a friend of the class. On a larger scale, using the friend keyword, one class can declare another class to be a friend. In doing so, one class can make all of its members available to the friend class. For example, if A is a class and wants to make class B a friend, then within A's class definition the following syntax would be used:

friend class B;

Under these circumstances B would now be a friend of A and can access all of its members.

It is important to note that friendship is not symmetric. If A is to be a friend of B, then

friend class A;

would have to be declared within B's class definition.


4e. Use class inheritance to design better code

  • What is inheritance?
  • What members can be accessed by a child class?
  • What is the syntax defining a parent child relationship?

Inheritance means that it is possible to define a parent child relationship between classes so that a child class will inherit all the attributes of its parent class (also referred to as the base class). Additionally, the child class can have its own data attributes and methods. Inheritance can be applied whenever the child has an is-a relationship to the parent class. For example, a square is a rectangle, a human is an animal, a car is a vehicle. Hence, a rectangle base class can be constructed where a square child class could inherit the rectangle class attributes.

The question of which members of a parent class can be accessed by a child class is a bit involved and its depth should be held off until the sections dealing with encapsulation and access modifiers are mastered. However, the most basic invocation of a child class B derived from a parent class A would look like:

class B: public A {class B definitions go here}

This is an example of public inheritance. Using this syntax, public and protected members will be accessible to the child class (base class private members, by definition and intentionally, are not accessible). In addition, it is extremely important to recognize that, if there are friend functions or friend classes, these are not inherited. Another point to note is that base class constructors and destructors are not automatically inherited.

Finally, when a base class has a single child, this is referred to as single inheritance. When a child class is derived from two or more base classes then it is called multiple inheritance. When the inheritance tree involves both single and multiple inheritance, this is referred to as hybrid inheritance.

To review, see Basics of Inheritance in C++ With Examples and Encapsulation, Inheritance, and Polymorphism In C++.


4f. Explain how polymorphism is achieved through C++ code

  • What is polymorphism?
  • How are polymorphisms used in C++?
  • What are some important concepts regarding polymorphism implementation?

Polymorphism means that different member function calls can be executed depending upon the type of object that calls the method. Polymorphisms typically arise when there is a hierarchy of classes related by inheritance. Under these circumstances, child classes can have separate implementations of a method having the same name or purpose. The purpose is often phrased as virtual function within a base class.

A classic type of example is to have a shape class to be considered as an abstract base class. The intent of an abstract class is not for instantiating objects, but rather to define overarching properties that will be inherited by child classes. For example, shapes such as a rectangle and a triangle could be child classes. All shapes might want to invoke an area method; however, each class would have its own method for doing so. The child class inheriting the area property would then be polymorphic. Furthermore, it is often appropriate for the abstract base class to contain a virtual method for the area to define its properties.

Lastly, a common technique for invoking the parent object is to create a pointer to the object. Using this technique, a major consequence of class inheritance is that a pointer to a derived class is type-compatible with a pointer to its base class. The feature is exactly why polymorphism works.

To review, see Encapsulation, Inheritance, and Polymorphism In C++ and Polymorphism.


4g. Create accessors, mutators, and access modifiers

  • What is an accessor?
  • What is a mutator?
  • What is an access modifier?

One major goal of encapsulation in object oriented programming is to define levels of attribute accessibility within a class definition. Access modifiers (also known as access specifiers) such as public, protected and private are used for this purpose. Public means that class objects can access a public member. Protected means that base class and inherited class objects can access a protected member. Private means that only class members can access a private member. Any attempt by an object to access a private method or data attribute will result in an error.

If you want an object to see private data, you need an accessor method (also known as a getter) within the class definition. An accessor generally consists of a single instruction that returns the data attribute. If you want an object to set a private data attribute, you need to include a mutator method (also known as a setter) within the class definition. Mutators return nothing (i.e. have void return type) and consist of a single instruction that assigns an input value to the data attribute. As a convention, the name of a getter usually begins with 'get' and the name of a setter usually begins with 'set'.

To review, see Accessors and Modifiers and Encapsulation, Inheritance, and Polymorphism In C++.


Unit 4 Vocabulary

This vocabulary list includes terms that students need to know to successfully complete the final exam for the course.

  • accessor method
  • base class
  • child class
  • constructor
  • derived class
  • destructor
  • encapsulation 
  • friend functions
  • getter
  • inheritance
  • mutator method
  • mutators 
  • operator overloading
  • parent class
  • pointer
  • polymorphism
  • private member
  • protected member
  • public inheritance 
  • setter
  • static attributes
  • static member
  • virtual function