I'm teaching myself C++ from the book "The Waite Group's OOP in C++". This is one of the exercises in Inheritance. The 2 classes, Counter and CountDn were supplied by the book. I had to add constructors to CountDN or the class wouldn't compile. I was to add the class CountPost to add postfix increment and decrement to the class Counter though inheritance by deriving the new class from CountDn.
My problem is, the main function can access the overloaded operators from CountPost but not from CountDn or Counter. Any hints about what I'm doing wrong would be great.
Giljo11778
Compiler: DEV-C++ 4.9.9.2 running on Windows XP Pro
include <iostream> // standard IO
using namespace std;
// from the book
class Counter //base class
{
protected:
unsigned int count;
public:
Counter() : count(0)
{ }
Counter(int c) : count(c)
{ }
unsigned int get_count() const
{ return count; }
Counter operator ++ ()
{ return Counter(++count); }
};
class CountDn : public Counter //class derived from Counter
{
public: // added constructors so the class would compile
CountDn () : Counter ()
{ }
CountDn (int c) : Counter(c)
{ }
Counter operator -- ()
{ return Counter(--count); }
};
// Class I added for the assignment
class CountPost : public CountDn //class derived from CountDn
{
public:
CountPost () : CountDn ()
{ }
CountPost (int c) : CountDn(c)
{ }
Counter operator -- (int) //decr count (prefix)
{ return Counter(count--); }
Counter operator ++ (int) //decr count (prefix)
{ return Counter(count++); }
};
int main() // shortened up main()
{
CountPost c1;
c1++;
c1--;
// Compiler errors on next line
--c1;
++c1;
return 0;
}
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\countenpostfixclass.cpp" -o "C:\countenpostfixclass.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\countenpostfixclass.cpp: In function `int main()':
C:\countenpostfixclass.cpp:98: error: no match for 'operator--' in '--c1'
C:\countenpostfixclass.cpp:84: note: candidates are: Counter CountPost::operator--(int)
C:\countenpostfixclass.cpp:99: error: no match for 'operator++' in '++c1'
C:\countenpostfixclass.cpp:86: note: candidates are: Counter CountPost::operator++(int)
Execution terminated
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The problem is resolved. I tried using the "using CountDn::operator ++" notation in CountPost but that could not resolve lines that would return a value like c3=++c2 because I can't store a CountDn object in a CountPost object. In the end I had to include the 2 overloaded operators from the Counter and CountDn classes in the CountPost class. Another option would have been to rewrite the base class to include these operators.
Thanks for everyone's advise, I learned a few things not covered in the book. And thanks for the book advise as well.
Giljo11778
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I suppose, that Clifford or Soma, much more fluent in english that me, can give you a detailed explanation, but I see that still you have some basic errors.
Old Newbie
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Declaring new methods with the same name as an inherited method, regardless of parameters, in a derived class always hide all inherited methods by that name. The solution is to use a 'using' directive for each inherited name that you wish the outside world to "see" all methods inherited and derived or overloaded.
Absolutely agree, and even more. Thinking in the matter, y don't believe this can be a innocent question, but a little trick.
Although the compiler don't complain there, I neither believe that the sentence that I comment in my response (in the Counter class) were taken "From the book". If so, I would say to OP, fire it!
Old Newbie.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your input Old Newbie, I see where you were going with the changes you made and I believe that would solve the compilation problem, however, the exercise in the book was to show the use of inheritance. By moving all the overload operators to the bottom class, we bypass the objective.
I've made some changes and now most of it works except:
CountPost cp3 = --cp2;
The problem seems to be in the overload -- prefix operator, it returns an object of CountDn and that cannot be stored in and object of type CountPost.
Here is the latest code and compile log, thank again for help.
class Counter { //base class
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count() { } //no-arg constructor
Counter(int c) : count(c) { } //1-arg constructor
unsigned int get_count() const { return count; } //return count
Counter operator ++ () { return Counter(++count); } //incr count (prefix)
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter //derived class
{
public:
CountDn () : Counter () { }
CountDn (int c) : Counter(c) { }
CountDn operator -- () { return CountDn(--count); } //decr count (prefix)
};
////////////////////////////////////////////////////////////////
class CountPost : public CountDn { //derived class
public:
CountPost () : CountDn () { }
CountPost (int c) : CountDn(c) { }
using Counter::operator ++; // found in an older post in this forum
using CountDn::operator --;
CountPost operator -- (int) { return CountPost(count--); } //decr count (prefix)
CountPost operator ++ (int) { return CountPost(count++); } //incr count (prefix)
};
int main() {
CountPost c1; //class CountPost
CountPost c2(100);
cout << "\nc1=" << c1.get_count(); //display
cout << "\nc2=" << c2.get_count(); //display
++c1; ++c1; ++c1; //increment cp1
cout << "\nc1=" << c1.get_count(); //display it
--c2; --c2; //decrement cp2
cout << "\nc2=" << c2.get_count(); //display it
// Errors on the next line
CountPost c3 = --c2; //create cp3 from cp2
cout << "\nc3=" << c3.get_count(); //display cp3
return 0;
}
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.cpp" -o "C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.cpp: In function int main()':
C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.cpp:93: error: conversion fromCountDn' to non-scalar type `CountPost' requested
Execution terminated
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Soma, I agree, this book I'm using has some problems, the least of which is poor examples. It instructs you in chapter 7 to use ostrstream to solve a problem when ostrstream is not discussed until chapter 12 as well as having spelt it wrong. I looked for 3 hours in the internet to find the proper usage and spelling.
Can you or anyone else suggest a better book for someone who has some outdated programming knowledge (haven't written code in over a decade) that is learning C++ and OOP for the 1st time?
Thanks for your time
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I haven't read many books about programming, but I like, and have learned a lot as beginner with "Thinking in C++,
Volume 1, 2nd Edition" by Bruce Eckel. It has some free version at http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
It's only a little slow to use, because it's big (7 or 800 pages).
Learner.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks Learner, I've downloaded those books. They look good for me but I still need a good beginners book for other people in my office that have no programming experience. Any suggestions from anyone would be great.
Giljo11778
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In the thread titled "Please Read Before Posting a Question", in the useful links section, there are pointers to a number of free online learning resources that you might find helpful.
With respect to textbooks, everyone has their favorites for various purposes, and books that one person may rave about - another person may think they are terrible. With respect to individuals, I firmly recommend people (a) Go down to the bookstore on a lazy day and take some time to look at and read books and (b) Be prepared to buy more than one book - even if you are perfect in predicting a book that is good for you, you need to realize that some books are good textbooks, others are good references, and others tell you about particulars of certain IDE's.
Amazon.com reviews can be helpful as well, just filter the "This book sucks" simplistic reviews from the ones that say effectively what works and doesn't.
The textbook I learned from is "C++ Primer Plus" by Stephen Prata. I like it, though some others do not. I generally avoid "For Dummies" books. I probably have 25 +/- C and C++ books, with related OO type books. Soma has more (and I am sure he has read each), and has done a lot of reading of online resources, so look carefully at what he says.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-04-20
>> I suppose, that Clifford or Soma...
Actually I gave up...
>> Declaring new methods with the same name as an inherited method,
>> regardless of parameters, in a derived class always hide all inherited
>> methods by that name.
... because I didn't know that. :-\
I started writing about all the other things that were just wrong in it, but did not bother to post since I had no solution to the actual problem.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm teaching myself C++ from the book "The Waite Group's OOP in C++". This is one of the exercises in Inheritance. The 2 classes, Counter and CountDn were supplied by the book. I had to add constructors to CountDN or the class wouldn't compile. I was to add the class CountPost to add postfix increment and decrement to the class Counter though inheritance by deriving the new class from CountDn.
My problem is, the main function can access the overloaded operators from CountPost but not from CountDn or Counter. Any hints about what I'm doing wrong would be great.
Giljo11778
Compiler: DEV-C++ 4.9.9.2 running on Windows XP Pro
include <iostream> // standard IO
using namespace std;
// from the book
class Counter //base class
{
protected:
unsigned int count;
public:
Counter() : count(0)
{ }
Counter(int c) : count(c)
{ }
unsigned int get_count() const
{ return count; }
Counter operator ++ ()
{ return Counter(++count); }
};
class CountDn : public Counter //class derived from Counter
{
public: // added constructors so the class would compile
CountDn () : Counter ()
{ }
CountDn (int c) : Counter(c)
{ }
Counter operator -- ()
{ return Counter(--count); }
};
// Class I added for the assignment
class CountPost : public CountDn //class derived from CountDn
{
public:
CountPost () : CountDn ()
{ }
CountPost (int c) : CountDn(c)
{ }
Counter operator -- (int) //decr count (prefix)
{ return Counter(count--); }
Counter operator ++ (int) //decr count (prefix)
{ return Counter(count++); }
};
int main() // shortened up main()
{
CountPost c1;
c1++;
c1--;
// Compiler errors on next line
--c1;
++c1;
return 0;
}
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\countenpostfixclass.cpp" -o "C:\countenpostfixclass.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\countenpostfixclass.cpp: In function `int main()':
C:\countenpostfixclass.cpp:98: error: no match for 'operator--' in '--c1'
C:\countenpostfixclass.cpp:84: note: candidates are: Counter CountPost::operator--(int)
C:\countenpostfixclass.cpp:99: error: no match for 'operator++' in '++c1'
C:\countenpostfixclass.cpp:86: note: candidates are: Counter CountPost::operator++(int)
Execution terminated
The problem is resolved. I tried using the "using CountDn::operator ++" notation in CountPost but that could not resolve lines that would return a value like c3=++c2 because I can't store a CountDn object in a CountPost object. In the end I had to include the 2 overloaded operators from the Counter and CountDn classes in the CountPost class. Another option would have been to rewrite the base class to include these operators.
Thanks for everyone's advise, I learned a few things not covered in the book. And thanks for the book advise as well.
Giljo11778
Try this:
include <cstdlib>
include <iostream>
include <new>
using namespace std;
// from the book
class Counter { //base class
public:
Counter() : count(0) { }
Counter(int c) : count(c) { }
unsigned int get_count() const { return count; }
// Counter operator ++ () { return Counter(++count); }
protected:
unsigned int count;
};
class CountDn : public Counter { //class derived from Counter
public: // added constructors so the class would compile
CountDn () : Counter () { }
CountDn (int c) : Counter(c) { }
// Counter operator -- () { return Counter(--count); }
};
// Class I added for the assignment
class CountPost : public CountDn { //class derived from CountDn
public:
CountPost () : CountDn () { }
CountPost (int c) : CountDn(c) { }
Counter operator -- (int) { return Counter(count--); } //decr count (postfix)
Counter operator ++ (int) { return Counter(count++); } //incr count (postfix)
Counter& operator -- () { Counter(--count); return this; }
Counter& operator ++ () { Counter(++count); return this; }
};
int main(int argc, char *argv[]) {
CountPost c1;
c1++;
c1--;
--c1;
++c1;
}
I suppose, that Clifford or Soma, much more fluent in english that me, can give you a detailed explanation, but I see that still you have some basic errors.
Old Newbie
>> I suppose, that Clifford or Soma...
Indeed also much more people in this forum.
Old Newbie.
Declaring new methods with the same name as an inherited method, regardless of parameters, in a derived class always hide all inherited methods by that name. The solution is to use a 'using' directive for each inherited name that you wish the outside world to "see" all methods inherited and derived or overloaded.
[code]
struct b
{
void test(int);
};
struct d: public b
{
using b::test;
void test(char *);
};
[/code]
Soma
Hint: The names in this case are: 'operator ++' and 'operator --'.
As a matter of interest, this is crap OO.
Soma
>> As a matter of interest, this is crap OO.
Absolutely agree, and even more. Thinking in the matter, y don't believe this can be a innocent question, but a little trick.
Although the compiler don't complain there, I neither believe that the sentence that I comment in my response (in the Counter class) were taken "From the book". If so, I would say to OP, fire it!
Old Newbie.
Thanks for your input Old Newbie, I see where you were going with the changes you made and I believe that would solve the compilation problem, however, the exercise in the book was to show the use of inheritance. By moving all the overload operators to the bottom class, we bypass the objective.
I've made some changes and now most of it works except:
CountPost cp3 = --cp2;
The problem seems to be in the overload -- prefix operator, it returns an object of CountDn and that cannot be stored in and object of type CountPost.
Here is the latest code and compile log, thank again for help.
class Counter { //base class
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count() { } //no-arg constructor
Counter(int c) : count(c) { } //1-arg constructor
unsigned int get_count() const { return count; } //return count
Counter operator ++ () { return Counter(++count); } //incr count (prefix)
};
////////////////////////////////////////////////////////////////
class CountDn : public Counter //derived class
{
public:
CountDn () : Counter () { }
CountDn (int c) : Counter(c) { }
CountDn operator -- () { return CountDn(--count); } //decr count (prefix)
};
////////////////////////////////////////////////////////////////
class CountPost : public CountDn { //derived class
public:
CountPost () : CountDn () { }
CountPost (int c) : CountDn(c) { }
using Counter::operator ++; // found in an older post in this forum
using CountDn::operator --;
CountPost operator -- (int) { return CountPost(count--); } //decr count (prefix)
CountPost operator ++ (int) { return CountPost(count++); } //incr count (prefix)
};
int main() {
CountPost c1; //class CountPost
CountPost c2(100);
cout << "\nc1=" << c1.get_count(); //display
cout << "\nc2=" << c2.get_count(); //display
++c1; ++c1; ++c1; //increment cp1
cout << "\nc1=" << c1.get_count(); //display it
--c2; --c2; //decrement cp2
cout << "\nc2=" << c2.get_count(); //display it
// Errors on the next line
CountPost c3 = --c2; //create cp3 from cp2
cout << "\nc3=" << c3.get_count(); //display cp3
return 0;
}
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.cpp" -o "C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.cpp: In function
int main()': C:\Cprojects\OOP in C++ examples created in bloodshed DEV C++\chapter 9 exercise 7\counten postfix class.cpp:93: error: conversion from
CountDn' to non-scalar type `CountPost' requestedExecution terminated
Hi Soma, I agree, this book I'm using has some problems, the least of which is poor examples. It instructs you in chapter 7 to use ostrstream to solve a problem when ostrstream is not discussed until chapter 12 as well as having spelt it wrong. I looked for 3 hours in the internet to find the proper usage and spelling.
Can you or anyone else suggest a better book for someone who has some outdated programming knowledge (haven't written code in over a decade) that is learning C++ and OOP for the 1st time?
Thanks for your time
I haven't read many books about programming, but I like, and have learned a lot as beginner with "Thinking in C++,
Volume 1, 2nd Edition" by Bruce Eckel. It has some free version at http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
It's only a little slow to use, because it's big (7 or 800 pages).
Learner.
Thanks Learner, I've downloaded those books. They look good for me but I still need a good beginners book for other people in my office that have no programming experience. Any suggestions from anyone would be great.
Giljo11778
In the thread titled "Please Read Before Posting a Question", in the useful links section, there are pointers to a number of free online learning resources that you might find helpful.
With respect to textbooks, everyone has their favorites for various purposes, and books that one person may rave about - another person may think they are terrible. With respect to individuals, I firmly recommend people (a) Go down to the bookstore on a lazy day and take some time to look at and read books and (b) Be prepared to buy more than one book - even if you are perfect in predicting a book that is good for you, you need to realize that some books are good textbooks, others are good references, and others tell you about particulars of certain IDE's.
Amazon.com reviews can be helpful as well, just filter the "This book sucks" simplistic reviews from the ones that say effectively what works and doesn't.
The textbook I learned from is "C++ Primer Plus" by Stephen Prata. I like it, though some others do not. I generally avoid "For Dummies" books. I probably have 25 +/- C and C++ books, with related OO type books. Soma has more (and I am sure he has read each), and has done a lot of reading of online resources, so look carefully at what he says.
Wayne
>> I suppose, that Clifford or Soma...
Actually I gave up...
>> Declaring new methods with the same name as an inherited method,
>> regardless of parameters, in a derived class always hide all inherited
>> methods by that name.
... because I didn't know that. :-\
I started writing about all the other things that were just wrong in it, but did not bother to post since I had no solution to the actual problem.
Clifford