Menu

Friday, 15 May 2015

Chapter 9, Problem 7: OOP By Robert Lafore

Problem Statement:

Start with the COUNTEN2 program in this chapter. It can increment or decrement a
counter, but only using prefix notation. Using inheritance, add the ability to use postfix
notation for both incrementing and decrementing. (See Chapter 8 for a description of
postfix notation.)

Solution:

//Ahmad Furqan
//P9.7
#include <iostream>
#include <conio.h>
using namespace std;
////////////////////////////////////////////////////////////////
class Counter
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count() //constructor, no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{
return count;
}
Counter operator ++ () //incr count (prefix)
{
return Counter(++count);
}
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter
{
public:
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c) : Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{
return CountDn(--count);
}
};
////////////////////////////////////////////////////////////////
class Countupdown :public CountDn
{
public:
Countupdown() :CountDn()
{}
Countupdown(int c) :CountDn(c)
{}
Countupdown operator ++(int)
{
return Countupdown(count++);
}
Countupdown operator --(int)
{
return Countupdown(count--);
}
};
////////////////////////////////////////////////////////////////
void main(void)
{
CountDn c1; //class CountDn
CountDn c2(100);
cout << "\nc1 = " << c1.get_count(); //display
cout << "\nc2 = " << c2.get_count(); //display
++c1; ++c1; c1++; //increment c1
cout << "\nc1 = " << c1.get_count(); //display it
--c2; c2--; //decrement c2
cout << "\nc2 = " << c2.get_count(); //display it
CountDn c3 = c2--; //create c3 from c2
cout << "\nc3 = " << c3.get_count(); //display c3
cout << endl;
_getch();
}

1 comment:

  1. Alderbaba - Stainless Steel - TITanium Arts
    Titanium Arts is a gemstone-cutting ford ecosport titanium craft with the help of modern materials. This glass is crafted with the help of our toaks titanium 750ml pot glass tools and  Rating: 4.5 · ‎1 titanium easy flux 125 review · ‎$49.99 · ‎In titanium jewelry stock titanium teeth dog

    ReplyDelete