[an error occurred while processing this directive]
CHAPTER 5 COMMON MISTAKES MADE WITH CLASSES
How do I...
- 5.1 Know when to take an object-oriented approach or a procedural approach?
- 5.2 Use and access a classs data members?
- 5.3 Use the scope resolution operator?
- 5.4 Use dot notation to access the member functions of an object?
- 5.5 Know which constructor to use when there are several to choose from?
- 5.6 Implement function overloading?
- 5.7 Correctly make use of inheritance?
- 5.9 Pass parameters back through the C++ inheritance mechanism to parent classes?
- 5.9 Distinguish between virtual classes and nonvirtual classes? How do I know when to use virtual classes and what does the word virtual mean in C++?
After a programmer progresses to the levels of object-oriented programming (OOP), whether new to programming or coming from a non-OOP language, she immediately discovers common mistakes made over and over again by many programmers. Often, it is because C++ is such a large language to learn that the underlying basics of OOP are left out. It is these basic ideas that trap beginning OOP programmers in the early stages and leave them scratching their heads. Often, they are left wondering what the error message actually means and also whether there is anyone around who understands it. This chapter is designed to help you understand the basic ideas of OOP and covers some of the more common mistakes made by beginning C++ programmers.
5.1 Know When to Take an Object-Oriented Approach or a Procedural Approach
When writing a program using an object-oriented language, many new programmers dont know whether to write the program using objects or in a procedural fashion. Often, many think that because C++ is object-oriented, all programs should be written using objects. That is not true and often programs dont need to be written in an object style. It is important to learn when to use objects and when not to.
5.2 Use and Access a Classs Data Members
A class can have data members (variables). It is these members that store the data internally to their class. This method encapsulates the data inside the class in a tightly bound, single entity. When an instance of a class is created, these data members are also created with it and given their own space in memory. For each instance of the class, a new set of data members is created. However, because they are stored within a class, they only exist inside of an object at runtime and can only be accessed via that object. How are values assigned to them?
5.3 Use the Scope Resolution Operator
The scope resolution operator has two main functions. First, it can be used in a program to access a global variable that has the same name as a local variable. By placing the scope resolution operator in front of the variable, you can directly control which variable the compiler uses. The second use is within object-oriented programs. The scope resolution operator is used to identify which class a particular function is a member of. This allows classes to share function names but keep each one separate in the program.
5.4 Use Dot Notation to Access the Member Functions of an Object
In object-oriented programs, the use of the dot operator is vital to access member functions. Often new C++ programmers either forget or dont know where or how to use the operator. The use of the dot is often confused when calling standard functions mixed with member functions. It is the member functions that require the dot and not the standard functions.
5.5 Know Which Constructor to Use When There Are Several to Choose From
Classes often use more than one constructor to gather data. Because each constructor has a different signature, it is important to know how to access each one. To understand this, function overloading should be addressed as the constructors each have the same name but will have different parameters. By using multiple constructors, the programmer can increase the usefulness of the class because he can control the amount of data passed to the class.
5.6 Implement Function Overloading
In programs, you quite often need a function that can perform several different tasks but use the same interface. Overloaded functions are the simple solution. For example, you might have several functions using the same name but handling different data types. The programmer would then have one common interface but be able to use mixed data types.
5.7 Correctly Make Use of Inheritance
Inheritance is a very useful and powerful tool in any object-oriented programming language. It gives the programmer access to pre-written classes that perform specific jobs. By utilizing inheritance, the programmer can rapidly create programs knowing there will be minimal debugging time. This is because any classes inherited by the program will (in theory) be pre-tested. The main problem people find when working with inheritance is simply how to use it in the first place.
5.8 Pass Parameters Back Through the C++ Inheritance Mechanism to Parent Classes
Beginning C++ programmers find writing inherited constructors very difficult. It can be very confusing to know which parameters are used in the derived class and which ones to pass on to the base class. For a hierarchical structure to work, there must be a coherent structure wherein all parameters have a destination.
5.9 Distinguish Between Virtual Classes and Nonvirtual Classes? How Do I Know When to Use Virtual Classes and What Does the Word Virtual Mean in C++?
When building up a hierarchical inheritance network, it is difficult to know when to use virtual and nonvirtual classes. The difference is in the way the classes are inherited. Nonvirtual inheritance means the derived class makes a copy of the base class. Inheriting a virtual class means that only a reference to the base class is used. There is no actual copy, only an image.
5.1 Know when to take an object-oriented approach or a procedural approach?
Problem
Both OOP- and procedural-style programs seem to achieve the same results as far as input and output are concerned. In addition, to the beginning OOP programmer, object-oriented programming might seem long-winded and confusing whereas procedural programming might seem direct and concise.
Technique
To point out the differences, you will attempt to solve the following problem:
Write a program that will accept two letters as input, compare these two letters, and then determine whether these two letters are the same or not. A message stating the result must be displayed to the screen.
|