I am not able to solve 3 error . Programs implementation is right but not sure how to get rid of 3 errors
# include <iostream.h>
# include < conio.h>
void main() {
class coord {
float x;
float y;
//Constructor
coord(float init_x,float init_y) {
x= init_x;
y= init_y;
}
void set(float f1, float f2) {
x = f1;
y = f2;
}
float get_x() {return x;}
float get_y() {return y;}
virtual void plot() {
cout<<x;
cout<<y;
};
class 3d_coord: public coord {
float z;
//constructor
3d_coord(float init_x,float init_y,float init_z): coord(init_x,init_y) {
z= init_z;
}
void set(float f1,float f2,float f3) {
coord::set(f1, f2); z = f3;
}开发者_开发问答
float get_z() { return z; }
virtual void plot() {
coord::plot();
cout<<z;
};
int test
void *ptr;
cin>>test;
coord a(1.1, 2.2);
3d_coord b(3.0, 4.0, 5.0);
if (test)
ptr = &a;
else
ptr = &b;
ptr-> plot();
}
}
I can spot at least three errors:
The standard library header is
<iostream>
, not<iostream.h>
.<conio.h>
is not a C++ standard library header and is best avoided.main()
must returnint
, notvoid
.Standard library names (e.g.
cout
) are in thestd
namespace; you need to qualify them.
Since you don't say which errors you want solved, I don't know if these are them, but they are three errors nonetheless. Just in case, here are some bonus errors:
3d_coord
is not a valid class name; a class name must be an identifier, which means it must begin with a letter or an underscore, not a number.You shouldn't use inheritance to relate
coord
and3d_coord
(or whatever you choose to name it after you've fixed bonus error number 1). A three-dimensional coordinate is not a two-dimensional coordinate, even though they share two common members. Inheritance should be used for is-a relationships.After extracting data from a stream (
cin
, in this case), you must test to ensure the extraction succeeded.ptr
is of typevoid*
; you cannot call member functions through avoid*
(there are very few times where it is a good idea to use avoid*
in a C++ program at all).It's not really an error, but usually you don't define classes inside of functions (there are exceptions; functors, for example).
You don't put the class definition inside the main function, and you don't put the 3d_coord class inside the coord class.
I can spot one:
void *ptr;
...
ptr-> plot(); // void::plot() is not
cin>>test;
coord a(1.1, 2.2);
3d_coord b(3.0, 4.0, 5.0);
if (test)
ptr = &a;
else
ptr = &b;
ptr-> plot();
Doesn't appear to be a function...
精彩评论