Sign In | Subscribe
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 (3)

1 answer

Last reply by: Alvin Sylvain
Sun Nov 16, 2014 2:50 PM

Post by Ram Manohar Oruganti on November 13, 2014

Mr. Sylvain, I wrote this piece of code:
#pragma once
class aux
{
public:
int dummy;
aux(){dummy = 0;}
virtual ~aux(){}

};
class test
{
public:
int dummy;
test(void);
virtual ~test(void);
static unsigned int MRU_count;
static aux *MRU_block;
};

The above code throws errors without the following lines in the header files:
unsigned int test::MRU_count=0;
aux q;
aux *test::MRU_block = &q;

And it wouldn't allow me to define these in the main function, saying these can't be defined in the current scope. Why is that?
Also why am I supposed to mention the data type again? Shouldn't the compiler get it from class definition?

I am using Visual Studio, if that makes any difference. Thanks in advance.  

0 answers

Post by Ram Manohar Oruganti on November 12, 2014

So, Mr. Sylvain, I want to make sure that I have it right. Any class with a pure virtual function becomes abstract; is that correct?

Object Oriented Programming, Part 2

  • Add overloaded constructors for the “Vehicle” class you wrote in the last lesson
    • Keep one default constructor that sets all attributes to sensible values.
    • Add a constructor that sets the Vehicle’s name
    • Add a constructor that sets the Vehicle’s name and current location
    • Add a constructor that sets each of the Vehicle’s attributes
  • Initialize the attributes using an Initialization List
  • Add text to the Vehicle destructor that prints the Vehicle name and the fact that it is being destroyed
  • Repeat the questions above with the “Auto” class, and any other classes you wrote for the previous lesson.
  • Create a “friend” function for the “Auto” class called “inject_nitrous”. This function will multiply the current acceleration by 1.5 if it’s positive or 0.5 if it’s negative. Run this function in different places in your code to see its effect.
  • Create a “Driver” class, with attributes including the driver’s name, the sponsor’s name, and the number assigned for the race.
    • Have the “Driver” class be a “friend” of the “Auto” class
    • The “Driver” should not only call the “Auto’s” pedal pressing methods, but also an “inject_nitrous” method that works similarly to the function above.
    • Update the code so that the Driver is pressing the pedals.
  • Change the “Vehicle” to an abstract class. The “Auto” class can derive from it and you can instantiate and “Auto”. Note how this affects your design.
  • Divide all of the classes into separate compilation units. That is, you will need files for “Vehicle.h”, “Vehicle.cpp”, “Auto.h”, “Auto.cpp”, etc. The main function can be placed into a file named “RaceCarMain.cpp”.
    • Compile and run everything together and get it all working again.

Object Oriented Programming, Part 2

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:10
  • Overloading Constructors 3:10
    • Provide Other Constructors
    • Example
  • Initialization Lists 5:40
    • Syntax Example
    • Equivalent to Initializing in Statements in the Method Body
  • Destructors 7:09
    • Destructor is Automatically Called when Instance is Deleted
    • Declare the Declare as Virtual in the Event the Class is Designed to be Derived by Sub-Class
  • What's in a Name? 11:15
    • Example: How to Tell Field from Parameter
  • Friendship Functions 16:32
    • Avoids Strict Object Oriented Principles and Declare the Function or Class Using 'friend' Keyword
    • Example
  • Friendship Classes 18:48
    • Example
  • Designing for Derivation 20:54
    • Add 'Virtual' Keyword to Public Methods
    • Add 'Virtual' Keyword to Destructors
  • Abstract Class 25:41
    • Designed Only for Derivation
    • Example
  • Compilation Units 28:22
    • Classes Are Typically Divided into the Interface Portion and Implementation Portion
    • Namespaces Are Used to Associate a Class Method with the Class Definition
  • Example Compilation Units 30:53
    • Definition for 'Vehicle' Class for an Include File
    • Implementation for 'Vehicle' Class in Program File
  • Why You Add 'Include Guards' 36:33
    • Suppose You Derive 'Automobile' From 'Vehicle'
    • Caller File Includes Both
  • Object Oriented? Or Not? 38:46
    • When Is It Appropriate?
    • When Is It Not Appropriate?
  • The Object Oriented Purist 43:53