<< Chapter < Page Chapter >> Page >

Example:

const Date currentDate;

Note: Constant data members in a class can not be assigned values using a standard assignment statement. Therefore, you must use an initialization list to assign initial values to constant data members.

Example:

//Payroll.h

class Payroll{

public:

Payroll();

private:

const double dFedTax;

const double dStateTax;

};

//Payroll.cpp

#include “Payroll.h

#include<iostream.h>

Payroll::Payroll()

:dFedTax(0.28), dStateTax(0.05){

};

In contrast, the following code raises several compiler errors since constant data members must be initialized in an initialization list:

Example:

//Payroll.h

class Payroll{

public:

Payroll();

private:

const double dFedTax;

const double dStateTax;

};

//Payroll.cpp

#include “Payroll.h”

#include<iostream.h>

Payroll::Payroll( ){

dFedTax = 0.28; //illegal

dStateTax = 0.05; //illegal

};

Constant functions

Another good programming technique is to use the const keyword to declare get functions as constant function. Get functions are function members which do not modify data members.

The const keyword makes your programs more reliable by ensuring that functions that are not supposed to modify data cannot modify data. To declare a function as constant, you add the const keyword after a function’s parentheses in both the function declaration and definition.

//Payroll.h

double getStateTax(Payroll* pStateTax) const;

//Payroll.cpp

double Payroll::getStateTax(Payroll* pStateTax) const {

return pStateTax->dStateTax;

};

Inheritance

Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors, and overriding or embellishing these with capabilities the new classes require. Software reusability saves time in programming development. It encourages the reuse of proven and debugged high quality software, thus reducing problems after a system becomes functional.

Basic inheritance

Inheritance refers to the ability of one class to take on the characteristics of another class.

Often, classes do not have to be created “from scratch”. Rather, they may be derived from other classes that provide attributes and behaviors the new classes can use. Such software reuse can greatly enhance programmer productivity.

Base classes and derived classes

When you write a new class that inherits the characteristics of another class, you are said to be deriving or subclassing a class.

An inherited class is called the base class , or superclass and the class that inherits a base class is called a derived class or subclass .

A class that inherits the characteristics of a base class is said to be extending the base class since you often extend the class by adding your own class members.

When a class is derived from a base class, the derived class inherits all of the base class members and all of its member functions, with the exception of:

  • constructor functions
  • copy constructor functions
  • destructor functions
  • overloaded assignment (=) functions

A derived class must provide its own implementation of these functions.

Consider a class originally developed by a company to hold an individual’s data, such as an ID number and name. The class, named Person, contains three fields and two member functions.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask