Home > Back-end >  (turn) c about inheritance get dizzy
(turn) c about inheritance get dizzy

Time:10-23

Recently in the title of type c + +, always make mistakes about inheritance, before using the feeling is very good, some of the same code can pass some but not by very headache
Want to ask where there's a better interpretation of the
Such as this code calculates the distance to the origin: () is not in conformity with the result, only the output of the first input value
 # include 
#include
using namespace std;
The class point1d
{
Protected:
Float x;
Public:
Point1d (float a)
{
X=a;
}
Float short ()
{
If (x<0)
The return - x;
The else
return x;
}
};
The class point2d: public point1d
{
Protected:
Float y;
Public:
Point2d (float a, float b) : point1d (a)
{
X=a;
Y=b;
}
Float short ()
{
Float d=SQRT (x * * y x + y);
Return d;
}
};
The class point3d: public point2d
{
Protected:
Float z;
Public:
Point3d (float a, float b, float c) : point2d (a, b)
{
X=a;
Y=b;
Z=c;
}
Float short ()
{
Float d=SQRT (x * * y x + y + z * z);
return d;
}
};
Int main ()
{
The int type.
Cin> The type;
Point1d * p;
float a,b,c;
While (type!=0)
{
The switch (type)
{
Case 1:
Cin> a;
P=new point1d (a);
break;
Case 2:
Cin> a> b;
P=new point2d (a, b);
break;
Case 3:
Cin> a> b> c;
P=new point3d (a, b, c);
break;
}
Cout

Cin> The type;
}
}


There is more to the previous version:
 
#include
#include
using namespace std;
The class point1d
{
Protected:
Float x;
Public:
Point1d float (p=0.0);//the line commented out via
Float short ()
{
Cin> x;
If (x<0)
The return - x;
The else
return x;
}
};
Class point2d: public point1d//complains here
{
Protected:
Float y;
Public:
Float short ()
{
Cin> X> y;
Float d=SQRT (x * * y x + y);
return d;
}
};
The class point3d: public point2d
{
Protected:
Float z;
Public:
Float short ()
{
Cin> X> y> Z;
Float d=SQRT (x * * y x + y + z * z);
return d;
}
};
Int main ()
{
The int type.
Cin> The type;
While (type!=0)
{
Point1d * p;
Switch (type)//if this operation, the result is not in conformity with the
{
Case 1:
P=new point1d//if no this line will run this situation error
Cout

break;
Case 2:
P=new point2d;
Cout

break;
Case 3:
P=new point3d;
Cout

break;
}
}
}


TAT is stunned

CodePudding user response:

Reduce the number of inheritance may be better

CodePudding user response:

You this is the wrong use of the inheritance

CodePudding user response:

refer to the second floor truth is right or wrong response:
you that this is the wrong use of inheritance

So just for help, didn't understand so wrong

CodePudding user response:

reference Sy_Faker reply: 3/f
Quote: refer to the second floor truth is right or wrong response:

You this is the wrong use of the inheritance

So just for help, didn't understand so wrong

Your first question, is the inheritance,
According to the magnitude of rule: any base class can appear, subclasses can appear,
Your base class is point1, you think about it, any point1 *, whether can be replaced with point2 and point3???????
Obviously can't,
The second problem is that the problem of c/c + + language,
Type pointer variable to point to specific address can be calculated,
So after p must be new to short ()

CodePudding user response:

Again again come, I knock a code error
 # include 
#include
using namespace std;
The class time
{
Protected:
Int second;
Int minute;
Int hour;
Public:
Void init ()
{
Int h, m, s;
Cin> H> M> s;
getchar();
Hour=h;
Minute=m;
Second=s;
}
Void operator + + ()
{
Second++;
If (second==60)
{
Second=0;
Minute++;
}
If (minute==60)
{
Minute=0;
Hour++;
}
}
Void operator, ()
{
Second,;
If (second<0)
{
Second=59;
Minute -;
}
If (minute<0)
{
Minute=59;
Hour -;
}
}
};
The class time_12hours: public time
{
The string type.
String interval;
Public:
Void the print ()
{
Cout}
Void in (string STR)
{
The interval=STR;
}
Void operator, ()
{
If (hour<0)
Hour=12;
}
Void operator + + ()
{
If (hour==60)
Hour=0;
}
};
The class time_24hours: public time
{
The string type.
Public:
Void the print ()
{
Cout}
Void operator + + ()
{
If (hour==60)
Hour=0;
}
Void operator, ()
{
If (hour<0)
Hour=23; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related