<< 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.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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