B
bintom
In the first program, given below, I have to make "father" and
"mother" virtual. However, in the second program, even without
declaring the classes virtual, the program works. Why?
//
***********************************************************************************
// 1st Program
class person
{ char name[21]; int age;
public : void init(char *n, int a) { strcpy(name, n); age = a; }
char* get_name() { return name; }
int get_age() { return age; }
};
class father : virtual public person
{ char sport[11];
public : void init(char *n, int a, char *sp)
{ person :: init(n, a);
strcpy(sport, sp);
}
char* get_sport() { return sport; }
};
class mother : virtual public person
{ char pasttime[16];
public : void init(char *n, int a, char *p)
{ person :: init(n, a);
strcpy(pasttime, p);
}
char* get_pasttime() { return pasttime; }
};
class child : public father, public mother
{ char school[21];
public : void init(char *n, int a, char *sp, char *p, char *sc)
{ father :: init(n, a, sp);
mother :: init(n, a, p);
strcpy(school, sc);
}
char* get_school()
{ return school; }
};
int main()
{ person p; p.init("Jack", 23);
cout << p.get_name() << " " << p.get_age() << endl;
father f; f.init("Papa", 45, "Cricket");
cout << f.get_name() << " " << f.get_age() << " " << f.get_sport()
<< endl;
mother m; m.init("Mama", 42, "Gardening");
cout << m.get_name() << " " << m.get_age() << " " <<
m.get_pasttime() << endl;
child c; c.init("Kiddo", 12, "Kabaddi", "Bird watching", "BCRS");
cout << c.get_name() << " " << c.get_age() << " " << c.get_sport()
<< " "
<< c.get_pasttime() << " " << c.get_school() << endl;
}
//
***********************************************************************************
// 2nd Program
class Point
{ private: int x, y;
public : void setX(int X) { x = X; }
int getX() { return x; }
void setY(int Y) { y = Y; }
int getY() { return y; }
void moveX(int i) { setX(getX() + i); }
};
class Circle : public Point
{ private: float radius;
public : void setR(float R) { radius = R; }
float getR() { return radius; }
};
class Square : public Point
{ private: float side;
public : void setSide(float S) { side = S; }
float getSide() { return side; }
};
class Shape : public Circle, public Square
{ private: int color;
public : void setColor(int C) { color = C; }
int getColor() { return color; }
};
void shiftY(Point &P, int i)
{ P.setY(P.getY() + i); }
int main()
{ Square S1; S1.setX(9); S1.setY(4); S1.setSide(5.85);
cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() <<
"\n";
S1.moveX(11);
cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() <<
"\n";
shiftY(S1, 3);
cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() << "\n
\n";
}
"mother" virtual. However, in the second program, even without
declaring the classes virtual, the program works. Why?
//
***********************************************************************************
// 1st Program
class person
{ char name[21]; int age;
public : void init(char *n, int a) { strcpy(name, n); age = a; }
char* get_name() { return name; }
int get_age() { return age; }
};
class father : virtual public person
{ char sport[11];
public : void init(char *n, int a, char *sp)
{ person :: init(n, a);
strcpy(sport, sp);
}
char* get_sport() { return sport; }
};
class mother : virtual public person
{ char pasttime[16];
public : void init(char *n, int a, char *p)
{ person :: init(n, a);
strcpy(pasttime, p);
}
char* get_pasttime() { return pasttime; }
};
class child : public father, public mother
{ char school[21];
public : void init(char *n, int a, char *sp, char *p, char *sc)
{ father :: init(n, a, sp);
mother :: init(n, a, p);
strcpy(school, sc);
}
char* get_school()
{ return school; }
};
int main()
{ person p; p.init("Jack", 23);
cout << p.get_name() << " " << p.get_age() << endl;
father f; f.init("Papa", 45, "Cricket");
cout << f.get_name() << " " << f.get_age() << " " << f.get_sport()
<< endl;
mother m; m.init("Mama", 42, "Gardening");
cout << m.get_name() << " " << m.get_age() << " " <<
m.get_pasttime() << endl;
child c; c.init("Kiddo", 12, "Kabaddi", "Bird watching", "BCRS");
cout << c.get_name() << " " << c.get_age() << " " << c.get_sport()
<< " "
<< c.get_pasttime() << " " << c.get_school() << endl;
}
//
***********************************************************************************
// 2nd Program
class Point
{ private: int x, y;
public : void setX(int X) { x = X; }
int getX() { return x; }
void setY(int Y) { y = Y; }
int getY() { return y; }
void moveX(int i) { setX(getX() + i); }
};
class Circle : public Point
{ private: float radius;
public : void setR(float R) { radius = R; }
float getR() { return radius; }
};
class Square : public Point
{ private: float side;
public : void setSide(float S) { side = S; }
float getSide() { return side; }
};
class Shape : public Circle, public Square
{ private: int color;
public : void setColor(int C) { color = C; }
int getColor() { return color; }
};
void shiftY(Point &P, int i)
{ P.setY(P.getY() + i); }
int main()
{ Square S1; S1.setX(9); S1.setY(4); S1.setSide(5.85);
cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() <<
"\n";
S1.moveX(11);
cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() <<
"\n";
shiftY(S1, 3);
cout << S1.getX() << ", " << S1.getY() << ", " << S1.getSide() << "\n
\n";
}