#include <iostream> #include <string> using namespace std; class Point { protected: string type; double x, y; public: Point() : type("Point"), x(0), y(0) {} Point(double x, double y) : type("Point"), x(x), y(y) {} virtual ~Point() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Point& p) { os << "Type: " << p.type << endl; os << "Coordinates: (" << p.x << ", " << p.y << ")" << endl; return os; } virtual void PrintName() { cout << "Type: " << type << endl; } }; class Circle : public Point { protected: double r; public: Circle() : Point(), r(0) { type = "Circle"; } Circle(double x, double y, double r) : Point(x, y), r(r) { type = "Circle"; } virtual ~Circle() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Circle& c) { os << static_cast<const Point&>(c); os << "Radius: " << c.r << endl; return os; } virtual void PrintName() { cout << "Type: " << type << endl; } }; class Cylinder : public Circle { protected: double h; public: Cylinder() : Circle(), h(0) { type = "Cylinder"; } Cylinder(double x, double y, double r, double h) : Circle(x, y, r), h(h) { type = "Cylinder"; } virtual ~Cylinder() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Cylinder& cy) { os << static_cast<const Circle&>(cy); os << "Height: " << cy.h << endl; return os; } virtual void PrintName() { cout << "Type: " << type << endl; } }; int main() { Point p1(1, 2); cout << p1 << endl; Circle c1(1, 2, 3); cout << c1 << endl; Cylinder cy1(1, 2, 3, 4); cout << cy1 << endl; Point* p; p = &p1; p->PrintName(); p = &c1; p->PrintName(); p = &cy1; p->PrintName(); return 0; } 写出该程序的大致流程
时间: 2024-04-25 11:25:58 浏览: 56
1. 引入iostream和string头文件。
2. 声明Point类,包含类型(type)和坐标(x和y)属性,有默认构造函数和带参数构造函数,定义了一个友元函数用于输出对象信息,定义了一个虚函数PrintName用于输出对象类型。
3. 声明Circle类,继承自Point类,增加了半径(r)属性,有默认构造函数和带参数构造函数,定义了一个友元函数用于输出对象信息,重写了虚函数PrintName用于输出对象类型。
4. 声明Cylinder类,继承自Circle类,增加了高度(h)属性,有默认构造函数和带参数构造函数,定义了一个友元函数用于输出对象信息,重写了虚函数PrintName用于输出对象类型。
5. 在main函数中,分别创建一个Point对象、一个Circle对象和一个Cylinder对象,输出它们的信息。
6. 声明一个指向Point对象的指针p,在分别将p指向Point对象、Circle对象和Cylinder对象后,分别调用p的PrintName函数,输出对象类型。
7. 程序结束,析构Point、Circle和Cylinder对象。
相关问题
#include <iostream> #include <iomanip> #include <string> using namespace std; class Point { private: string type; double x, y; public: Point(string t = "", double xx = 0, double yy = 0) : type(t), x(xx), y(yy) {} Point(const Point& p) : type(p.type), x(p.x), y(p.y) {} virtual ~Point() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Point& p) { os << "Type: " << p.type << "\nX: " << fixed << setprecision(2) << p.x << "\nY: " << fixed << setprecision(2) << p.y; return os; } virtual void PrintName() { cout << "This is a Point object." << endl; } }; class Circle : public Point { private: double r; public: Circle(string t = "", double xx = 0, double yy = 0, double rr = 0) : Point(t, xx, yy), r(rr) {} Circle(const Circle& c) : Point(c), r(c.r) {} ~Circle() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Circle& c) { os << static_cast<const Point&>(c) << "\nRadius: " << fixed << setprecision(2) << c.r; return os; } virtual void PrintName() { cout << "This is a Circle object." << endl; } }; class Cylinder : public Circle { private: double h; public: Cylinder(string t = "", double xx = 0, double yy = 0, double rr = 0, double hh = 0) : Circle(t, xx, yy, rr), h(hh) {} Cylinder(const Cylinder& cy) : Circle(cy), h(cy.h) {} ~Cylinder() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Cylinder& cy) { os << static_cast<const Circle&>(cy) << "\nHeight: " << fixed << setprecision(2) << cy.h; return os; } virtual void PrintName() { cout << "This is a Cylinder object." << endl; } }; int main() { Point p1("Point1", 1, 2); Point p2("Point2", 3, 4); Circle c1("Circle1", 5, 6, 7); Circle c2("Circle2", 8, 9, 10); Cylinder cy1("Cylinder1", 11, 12, 13, 14); Cylinder cy2("Cylinder2", 15, 16, 17, 18); cout << p1 << endl; cout << p2 << endl; cout << c1 << endl; cout << c2 << endl; cout << cy1 << endl; cout << cy2 << endl; Point* ptr; ptr = &p1; ptr->PrintName(); ptr = &c1; ptr->PrintName(); ptr = &cy1; ptr->PrintName(); return 0; }c:\program files (x86)\microsoft visual c++6.0\myprojects\shiayn6\6.cpp(27) : error C2248: 'type' : cannot access private member declared in class 'Point' c:\program files (x86)\microsoft visual c++6.0\myprojects\shiayn6\6.cpp(8) : see declaration of 'type' 执行 cl.exe 时出错.
根据错误信息来看,你在第27行访问了 Point 类的私有成员变量 type,而私有成员变量只能在类内部访问,外部无法访问。在 Circle 和 Cylinder 类中,你使用了 Point 类的构造函数,所以需要在 Point 类中将 type 成员变量改为 protected,这样 Circle 和 Cylinder 类中就可以访问 type 成员变量了。你只需要将 Point 类的定义改为:
```c++
class Point {
protected:
string type;
double x, y;
public:
Point(string t = "", double xx = 0, double yy = 0) : type(t), x(xx), y(yy) {}
Point(const Point& p) : type(p.type), x(p.x), y(p.y) {}
virtual ~Point() { cout << type << " object is destroyed." << endl; }
friend ostream& operator<<(ostream& os, const Point& p) {
os << "Type: " << p.type << "\nX: " << fixed << setprecision(2) << p.x << "\nY: " << fixed << setprecision(2) << p.y;
return os;
}
virtual void PrintName() { cout << "This is a Point object." << endl; }
};
```
这样就可以解决这个问题了。
Define abstract class Employee, it contains: Data member: string id, string name, int age, double salary Constructor pay(): pure virtual function for calculating the employee’s salary Derived class Manager, it contains: Derived from class Employee. Data members: double fixedSalary Constructor Implement pay() function. Tips: The salary of managers = fixedSalary. Derived class Saleman, it contains: Derived from class Employee. Data member: double salesVolume Implement pay() function. Tips: Saleman salary = 0.04* salesVolume. Declare the base class pointer p and derived class object “Manager A("1001","Zhang San", 25, 5000)” and “Saleman B("1002", "Li Si", 28, 100000)” in the main function, and call the member functions pay() of different objects through the base class pointer.
Here is the implementation of the required classes and their functions in C++:
```c++
#include<iostream>
#include<string>
using namespace std;
// Abstract Employee class
class Employee {
protected:
string id;
string name;
int age;
double salary;
public:
Employee(string id, string name, int age, double salary) {
this->id = id;
this->name = name;
this->age = age;
this->salary = salary;
}
virtual void pay() = 0; // Pure virtual function
};
// Manager class derived from Employee
class Manager : public Employee {
private:
double fixedSalary;
public:
Manager(string id, string name, int age, double fixedSalary)
: Employee(id, name, age, 0) {
this->fixedSalary = fixedSalary;
}
void pay() {
salary = fixedSalary;
cout << "Manager " << name << " received salary: " << salary << endl;
}
};
// Salesman class derived from Employee
class Salesman : public Employee {
private:
double salesVolume;
public:
Salesman(string id, string name, int age, double salesVolume)
: Employee(id, name, age, 0) {
this->salesVolume = salesVolume;
}
void pay() {
salary = 0.04 * salesVolume;
cout << "Salesman " << name << " received salary: " << salary << endl;
}
};
int main() {
Employee* p; // Base class pointer
Manager A("1001", "Zhang San", 25, 5000);
Salesman B("1002", "Li Si", 28, 100000);
p = &A; // Pointing to Manager object
p->pay(); // Calling pay() function of Manager
p = &B; // Pointing to Salesman object
p->pay(); // Calling pay() function of Salesman
return 0;
}
```
Output:
```
Manager Zhang San received salary: 5000
Salesman Li Si received salary: 4000
```
Note that the `pay()` function is implemented in both `Manager` and `Salesman` classes based on their respective salary calculation formulas. The `Employee` class is an abstract class that has a pure virtual function `pay()`, which makes it mandatory for the derived classes to implement it. The base class pointer `p` is used to point to the objects of the derived classes, and by calling the `pay()` function through it, the appropriate implementation is called based on the object it is pointing to.
阅读全文