# can anyone find whats my misttake?

Printable View

• 03-21-2009
MyRedz
can anyone find whats my misttake?
hi i just done some coding about base class shape and derived class twod and threed..
can anyone show me my wrong?
Code:

```class Shape { public: Shape(){}; void print(); } class TwoD:public Shape { public: TwoD( double a =0.0,double b = 0.0): length(a),width(b){ setdata(a,b); } virtual double area() {return 0} void setdata(double a,double b){ length = a; width = b; } void print(){ cout<<length<<','<<width<<endl; } protected: double length,width; } class Threed:public TwoD{ friend ostream &operator<<(ostream &stream,Threed o); friend Threed operator*(int &Number,Threed &o); public: Threed operator++(); Threed operator*(int &Number); Threed(double a,double b,double c):TwoD(a,b),height(c){ setdata(a,b,c);} void print() { cout<<length<<','<<width<<','<<height<<endl; } void setdata( double a, double b,double c) { length = a; width = b; height = c; } protected: double height; } Threed Threed::operator++(){ length++;width++;height++;return *this; } Threed operator*(int &number,Threed &o) { Threed a; a.length= number*o.length; a.width= number*o.width; a.height= number*o.height; return a; ostream &operator<<(ostream &stream, Threed o) {   stream << o.length << " ";   stream << "(" << o.width << ") ";   stream << o.height << "-";   return stream; // must return stream } int main() { ThreeD a(1,2,3),b(1,2),c(1); ++a; c = a*2;//use member function overloading c= 2*a;//use global function overloading return 0; }```
• 03-21-2009
anon
At first glance you are missing a couple of semicolons, perhaps some brackets and braces, need to decide whether it is ThreeD or Threed, etc. Some of which would be more obvious if you indented your code properly.

You should know that class declarations end with ; (after the closing brace). From there on you just have to read the compiler errors and fix them.

Also, you wouldn't be overwhelmed with errors, if you compiled and tested your code more often.

Conceptually I'm not so sure whether a ThreeD shape is a kind of TwoD shape. Perhaps both might be inherited from Shape, and ThreeD can use an instance of TwoD as a member for width and length dimension.
• 03-21-2009
MyRedz
ok..from my meaning and questions it says.
shape is base class.
class twod inherits public from class shape
and class threed inherits public from class twod
in twod width and length are declared,
and in threed another height is declared;
so here's my revised coding
Code:

```#include <iostream> using namespace std; class Shape { public: Shape(){}; void print(); }; class TwoD:public Shape { public: TwoD( int a =0,int b = 0): length(a),width(b){ setdata(a,b); } void setdata(int a,int b){ length = a; width = b; } void print(){ cout<<length<<','<<width<<endl; } protected: int length,width; }; class Threed:public TwoD { friend ostream &operator<<(ostream &stream,Threed o); friend Threed operator*(int Number,Threed o); public: Threed operator++(); Threed operator*(int Number); Threed(int a =0,int b =0,int c =0):TwoD(a,b),height(c){ setdata(a,b,c); } void print() { cout<<length<<','<<width<<','<<height<<endl; } void setdata( int a, int b,int c) { length = a; width = b; height = c; } protected: int  height; }; Threed Threed::operator++(){ ++length; ++width; ++height; return *this; } Threed operator*(int number,Threed o) { Threed a; a.length= number*o.length; a.width= number*o.width; a.height= number*o.height; return a; } Threed Threed::operator*(int number) { Threed a,b; a.length=b.length*number; a.width=b.width*number; a.height=b.height*number; return a,b; } ostream &operator<<(ostream &stream, Threed o) {   stream << o.length <<endl;   stream << o.width << endl;   stream << o.height <<endl;   return stream; // must return stream } int main() { Threed a(1,2,3),b(1,2),c(1); ++a;//prefix overloading c = a*2;//use member function overloading c= 2*a;//use global function overloading return 0; }```
error is
:\Users\Guest\aa.cpp(84) : error C2248: 'length' : cannot access protected member declared in class 'TwoD'
C:\Users\Guest\aa.cpp(28) : see declaration of 'length'
C:\Users\Guest\aa.cpp(85) : error C2248: 'width' : cannot access protected member declared in class 'TwoD'
C:\Users\Guest\aa.cpp(28) : see declaration of 'width'
C:\Users\Guest\aa.cpp(86) : error C2248: 'height' : cannot access protected member declared in class 'Threed'
C:\Users\Guest\aa.cpp(56) : see declaration of 'height'
Error executing cl.exe.

aa.obj - 3 error(s), 0 warning(s)
• 03-21-2009
anon
Well, I can't reproduce this error with any compiler. Try rebuilding. The only thing wrong is:

Code:

```Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing.  All rights reserved. MODE:strict errors C++ C++0x_extensions "ComeauTest.c", line 72: warning: expression has no effect       return a,b;             ^```
And this is what your code would look like with indentation:

Code:

```#include <iostream> using namespace std; class Shape { public:     Shape() {};     void print(); }; class TwoD:public Shape { public:     TwoD( int a =0,int b = 0): length(a),width(b) {         setdata(a,b);     }     void setdata(int a,int b) {         length = a;         width = b;     }     void print() {         cout<<length<<','<<width<<endl;     } protected:     int length,width; }; class Threed:public TwoD {     friend ostream &operator<<(ostream &stream,Threed o);     friend Threed operator*(int Number,Threed o); public:     Threed operator++();     Threed operator*(int Number);     Threed(int a =0,int b =0,int c =0):TwoD(a,b),height(c) {         setdata(a,b,c);     }     void print() {         cout<<length<<','<<width<<','<<height<<endl;     }     void setdata( int a, int b,int c) {         length = a;         width = b;         height = c;     } protected:     int  height; }; Threed Threed::operator++() {     ++length;     ++width;     ++height;     return *this; } Threed operator*(int number,Threed o) {     Threed a;     a.length= number*o.length;     a.width= number*o.width;     a.height= number*o.height;     return a; } Threed Threed::operator*(int number) {     Threed a,b;     a.length=b.length*number;     a.width=b.width*number;     a.height=b.height*number;     return a,b; } ostream &operator<<(ostream &stream, Threed o) {     stream << o.length <<endl;     stream << o.width << endl;     stream << o.height <<endl;     return stream; // must return stream } int main() {     Threed a(1,2,3),b(1,2),c(1);     ++a;//prefix overloading     c = a*2;//use member function overloading     c= 2*a;//use global function overloading     return 0; }```
• 03-21-2009
MyRedz
ok...the code problem is i use protected for the three values..
and i already made it friend for class twod and threed..
so why it stilll cannot access from overloading ostream?
i dun want to delete protected keyword if i can still use it in my coding..
any ideas?
Code:

```#include <iostream> using namespace std; class Shape { public:     Shape() {};     void print(); }; class TwoD:public Shape {   friend ostream &operator<<(ostream &stream,TwoD &); public:     TwoD( int a =0,int b = 0): length(a),width(b) {         setdata(a,b);     }     void setdata(int a,int b) {         length = a;         width = b;     }     void print() {         cout<<length<<','<<width<<endl;     } protected:     int length,width; }; class Threed:public TwoD {     friend ostream &operator<<(ostream &stream,Threed o);     friend Threed operator*(int Number,Threed o); public:     Threed operator++();     Threed operator*(int Number);     Threed(int a =0,int b =0,int c =0):TwoD(a,b),height(c) {         setdata(a,b,c);     }     void print() {         cout<<length<<','<<width<<','<<height<<endl;     }     void setdata( int a, int b,int c) {         length = a;         width = b;         height = c;     } protected:     int  height; }; Threed Threed::operator++() {     ++length;     ++width;     ++height;     return *this; } Threed operator*(int number,Threed o) {     Threed a;     a.length= number*o.length;     a.width= number*o.width;     a.height= number*o.height;     return a; } Threed Threed::operator*(int number) {     Threed a;     a.length=length*number;     a.width=width*number;     a.height=height*number;     return a; } ostream &operator<<(ostream &stream, Threed o) {     stream << o.length <<endl;     stream << o.width << endl;     stream << o.height <<endl;     return stream; // must return stream } int main() {     Threed a(1,2,3),b(1,2),c(1);     ++a;//prefix overloading     c = a*2;//use member function overloading     c= 2*a;//use global function overloading         c.print();     return 0; }```
• 03-21-2009
tabstop
I don't think you've quite got the hang of what we're saying here. The code you've posted compiles beautifully without any problems about not being able to access things, and prints "4,6,8" when it runs. If you're having problems with some other code, then you'll have to provide details about that code.