Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Intermediate C++
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Bookmark and Share
Lecture Comments (2)

1 answer

Last reply by: Alvin Sylvain
Tue Nov 4, 2014 10:46 PM

Post by Ram Manohar Oruganti on November 4, 2014


In the slide for default constructor, the example contains a user definition of constructor. So, why do we call it the default constructor? It isn't provided by the compiler after all.

Also, I would be glad if you could illustrate the diamond problem with an example. I fail to understand as to what problems it might cause.

Thanks & Regards,

Object Oriented Programming, Part 1

  • Create a class for a “Vehicle”
    • Keep things simple for now, limit the Vehicle to a single dimension. That is, it can go forward or backward, but not left or right, up or down, Time Travel, etc.
    • Attributes to include at least, Name, Location, Velocity, Acceleration
    • Include “setters” and “getters” for each attribute
    • The default constructor sets everything to a non-moving vehicle with the name, “nameless”
    • Include a method “Print(ostream &o)” that prints all of the attributes of an instance to the given output stream – think of a sensible format that clearly identifies each attribute.
  • Write a program that instantiates a “Vehicle” class. Set the name of the vehicle to “My Vehicle”. Print the attributes of the “Vehicle”.
    • Play around with it.
    • Instantiate different vehicles in the program.
    • Set the different attributes to different values using the “setters”, print them out using “getters”
    • Print the values using the “Print” method
  • Let’s give the object a bit more control over its own state; the caller should no longer be responsible (they’ll probably mess things up anyway).
    • Option: add a method to “Reset” the Vehicle back to its initial state
    • Remove the “setters” for the Location and Velocity attributes (Just make them “private” for now). We don’t want callers having direct control over those.
    • Add an attribute called “deltaTime”, with a “setter” and “getter”.
    • Add a new method called “AdvanceTime”, to perform the following:
      • velocity += acceleration * deltaTime;
      • location += velocity * deltaTime;
  • Let’s put this vehicle through some paces to simulate the acceleration and de-acceleration.
    • To start our tests, write a program to instantiate a Vehicle, and implement at least five separate loops such as following:
       Vehicle v1("My Vehicle"); v1.setDeltaTime(0.5); v1.setAcceleration(0.0); // Loop 1, remain at rest a while for (int t = 0; t < 10; ++t) {   v1.AdvanceTime();   v1.Print(cout); } v1.setAcceleration(1.0); // Loop 2, accelerate to speed while (v1.getVelocity() < 55.0) {   v1.AdvanceTime();   v1.Print(cout); } v1.setAcceleration(0.0); // Loop 3, cruise for a while for (int t = 0; t < 10; ++t) {   v1.AdvanceTime();   v1.Print(cout); } v1.setAcceleration(-1.0); // Loop 4, de-accelerate, stop while (v1.getVelocity() > 0.0) {   v1.AdvanceTime();   v1.Print(cout); } v1.setAcceleration(0.0); // Loop 5, remain stopped for (int t = 0; t < 10; ++t) {   v1.AdvanceTime();   v1.Print(cout); }
    • Play around with those loops, set the acceleration and de-acceleration to different values. Add more loops to accelerate and de-accelerate at different points of the trip. Compare results with different values for deltaTime.
    • You might find the following values interesting:
      • deltaTime = 1.0
      • accelerate at 1.0 while velocity < 55.0
      • de-accelerate at -20.0 while velocity > 0.0
  • Of course, you knew at the beginning we were going to derive the “Vehicle” base class into sub-classes, right? Derive a class named “Auto” as a sub-class of “Vehicle”.
    • Feel free to change any “private” member to “protected” if the sub-class needs access to it
    • Add a new attribute, “LicenseNumber” with “setter” and “getter”.
    • Override the “Print” method to additionally print the license number
    • Add two methods for the “Auto” sub-class, “pressGas()” and “pressBrake()”.
    • Design “pressGas” to call the “setAcceleration” of the super-class “Vehicle” with a parameter that is translated from how hard the pedal is being pushed into a positive acceleration – come up with a formula to make that translation
    • Design “pressBrake” in a similar fashion, only it must be a negative acceleration
    • Pedals on a car can’t be pressed a “negative” amount. If the caller provides a negative parameter, ignore it, print an error message or throw an exception.
    • Furthermore, brakes will not make the car go backward. Test the current velocity, and if it is less than or equal to zero, set both it and acceleration to zero.
    • The caller should not have access to the “setAcceleration” method in “Vehicle”. Accelerating and de-accelerating the “Auto” class should be strictly via the pedals. One solution is to override the “setAcceleration” method in the “Auto” class. Have it do nothing or print an error message.
  • Modify your program to use the “Auto” class instead of the “Vehicle” class. Instead of setting acceleration (which the “Auto” class should not allow), set the gas pedal to accelerate and set the brake pedal to slow down.
  • Try your program with the “interesting values” in question 4.c.
  • Extra: Some race car drivers press both the gas and the brake pedals at the same time. They say it gives them more “control”. Our design can’t handle that – yet!
    • Add new attributes to the “Auto” class for the “gasPressedAmount” and “brakePressedAmount”.
    • Initialize them to zero in the constructor, and add them to the “Print” method.
    • Work out some formula of your own choosing for a private method that computes a new acceleration value based on the combined values of “gasPressedAmount” and “brakePressedAmount.”
      For example:
      acceleration = gasPressedAmount * 0.5 – brakePressedAmount * 1.5;
    • Modify the “pressGas” and “pressBrake” methods so that, along with whatever other validation they’re already doing, the amount pressed on the pedal is saved in the appropriate attribute, then make this calculation before calling the “setAcceleration” method of the “Vehicle” super-class.
    • Modify your test program to try out different combinations.
  • One of the features of an “Auto” not found in a “Vehicle” is a set of tires. Create a new class, “Tire” that an “Auto” may have four of.
    • The “Tire” should have at least the attributes, Position, RotationalSpeed, Pressure and Temperature, with appropriate “getters” and a “Print” method.
    • Position indicates Front-Left, Front-Right, Rear-Left, Rear-Right
    • Pressure and Temperature will not have “setters” as they need to be calculated by the Tire itself. They’ll need to be set to initial values in the constructor.
    • Calculate the Temperature and Pressure in the RotationalSpeed “setter”. Temperature increases with the rotational speed. The faster the tire turns, the hotter it gets. The hotter it gets, the higher the pressure goes up.
    • Extra: add attributes MaxPressure and Destroyed. Set Destroyed to “true” if the pressure is higher than the MaxPressure (meaning the Auto was driving too fast and blew out the tire). Decide how you want to handle that possibility.
  • Modify the “Auto” class to have four “Tire” objects.
    • Design Decision Time: do you want an array of four tires? E.g.: Tire tire[4];
      • Or do you want to list each tire separately? E.g.: Tire frontLeft, frontRight, rearLeft, rearRight;
      • Consider the advantages and disadvantages.
      • Do you want a Spare? Is that really essential to what we’re designing?
    • Override the “AdvanceTime” method of the Vehicle class. It should call the super-class method, then add a calculation for each tire’s RotationalSpeed as a function of the current velocity.
    • Change the “Print” method to also call the “Print” method of each Tire.
    • Add a “Copy” constructor that performs a “Deep Copy”, so that when one “Auto” object is assigned to another, all of the attributes are copied, plus each tire and all of the attributes of each tire.
    • Write some test code to make sure everything works the way you expect.

Object Oriented Programming, Part 1

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:14
  • Concepts 4:39
    • Historically Distinct Ways of Programming and Design
    • OOP Emphasizes Important Concepts
  • Some Terminology 9:53
    • Object
    • Class
    • Instance
  • Example 14:04
  • Abstraction 20:00
    • Abstraction Defines the Essential Characteristics of an Object
    • Emphasis on Essential
    • Classes are Defined with Characteristics That Distinguish One Class of Objects From Another
  • Encapsulation 22:23
    • Private Data
    • Public Methods
    • Protected Data
  • Inheritance 25:52
    • What Is Inheritance?
    • Example
  • Polymorphism 29:18
    • Example of Operator Overloading
    • Example of Parameter Signature
    • Methods Inherited From another Object Can be Overwritten and Customized to the New Object
  • Object Relationships 33:37
    • Consider Two Objects, Car and Vehicle
    • Consider Two Other Objects, Car and Tire
    • Object Motorcycle
  • Inheritance Example 35:27
  • Multiple Inheritance 39:24
    • Example
    • Be Careful for Diamond Problem
  • Multiple Inheritance Example 45:28
  • Constructors 46:55
    • Default Constructor
    • Copy Constructor
  • Default Constructor 50:33
    • Example
  • Shallow Copy 52:41
    • Example
  • Deep Copy 56:35
    • Example
  • Copy Constructor 1:01:34
  • Good Design? Or… 1:04:28