실린더 클래스가 인쇄 및 볼륨 기능을 제대로 수행하지 못하는 것 같습니다. 다음은 할당에 대한 지침입니다. 추상 기본 클래스 인 Shape라는 클래스를 디자인하십시오. Shape에는 두 개의 순수 가상 함수 인 printShapeName과 print가 있습니다.가상 함수 "Shape"Assign
셰이프에는 두 개의 다른 가상 함수, 영역 및 볼륨이 있으며 각 영역에는 0 값을 반환하는 기본 구현이 있습니다.
Point 클래스는 Shape의 이러한 구현을 상속합니다 (점의 면적과 부피는 모두 0 임). Point는 x와 y 좌표 private 멤버를가집니다.
클래스 원은 공개 상속을 사용하는 Point에서 파생됩니다. Circle의 볼륨은 0.0이므로 기본 클래스 멤버 함수 볼륨은 무시되지 않습니다. 서클에는 0이 아닌 영역이 있으므로이 클래스에서 영역 함수가 재정의됩니다. 돌아오고 Circle에 새로운 반지름을 할당하는 get 및 set 함수를 작성하십시오.
클래스 실린더는 공개 상속을 통해 Circle에서 파생됩니다. Cylinder에는 Circle과 다른 면적과 부피가 있으므로이 클래스에서는 면적 및 부피 함수가 모두 재정의됩니다. get 및 set 함수를 작성하여 높이를 반환하고 새 높이를 지정하십시오.
한 점, 한 원 및 한 원을 작성한 다음 결과를 인쇄하십시오.
//
// Shape.hpp
// HW6_VirtualFunctions
//
// Created by Aviv Fedida on 11/25/17.
// Copyright © 2017 Aviv Fedida. All rights reserved.
//
#ifndef Shape_hpp
#define Shape_hpp
#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
protected: // protected members
double width, height, radius, pi, x, y;
public:
void setWidth(double a); // prototype for width setter
void setHeight(double b); // prototype for height setter
void setX(double c);
void setY(double d);
void setRad(double r);
void setPi(double p);
double getWidth() { // get width to return only
return width;
}
double getHeight() { // get height to return only
return height;
}
double getX() {
return x;
}
double getY() {
return y;
}
double getRad() {
return radius;
}
double getPi() {
return pi;
}
// Create public virtual functions
virtual void printShapeName() = 0; // pure virtual for printing shape's name
virtual void print(double a, double b) = 0; // pure virtual print function
virtual double area() { // virtual area function returns default null
return 0;
}
virtual double volume() { // virtual default volume function returns null
return 0;
}
}; // END BASE CLASS -------------------------------------------------------------------------------------
class Point: public Shape {
// x & y coordinates needed for a point
public:
// Prototypes for print & set functions
void printShapeName();
void print(double a, double b);
}; // end first derived class ------------------------------------------------------------------------
class Circle: public Point {
public:
// Protoypes for print functions
void printShapeName();
void print(double r, double p);
// Prototypes for area & volume functions
double area(double r, double p);
}; // end second derived class ----------------------------------------------------------------------------
class Cylinder: public Circle {
public:
double area(double r, double p);
void printShapeName();
double volume(double r, double p, double h);
void print(double r, double p, double h);
}; // end third and final derived class --------------------------------------------------------------------------
// Some definitions outside classes
//-------------------------------------------------- BEGIN -----------------------------------------------------------
// Shape base class setter functions defined
void Shape::setWidth(double a) {
width = a;
}
void Shape::setHeight(double b) {
height = b;
}
void Shape::setX(double c) {
x = c;
}
void Shape::setY(double d) {
y = d;
}
void Shape::setRad(double r) {
radius = r;
}
void Shape::setPi(double p) {
p = 3.1416;
pi = p;
}
void Point::printShapeName() { // Print name of class
cout << "Point " << endl;
}
void Point::print(double a, double b) { // Print values within class
cout << "(" << a << "," << b << ")" << endl;
}
void Circle::printShapeName() {
cout << "Circle " << endl;
}
// Circle area function defined
double Circle::area(double r, double p) {
double area;
area = p*(pow(r,2));
return area;
}
void Circle::print(double r, double p) {
cout << "Area of circle is: " << area(r, p) << endl;
cout << "Volume of circle is: " << volume() << endl;
}
void Cylinder::printShapeName() {
cout << "Cylinder " << endl;
}
double Cylinder::area(double r, double p) {
double area;
area = 2*p*r;
return area;
}
double Cylinder::volume(double r, double p, double h) {
double volume;
volume = p*(pow(r,2))*h;
return volume;
}
void Cylinder::print(double r, double p, double h) {
cout << "Area of cylinder is: " << area(r, p) << endl;
cout << "Volume of cylinder is: " << volume(r, p, h) << endl;
}
#endif /* Shape_hpp */
//
// main.cpp
#include <iostream>
#include "Shape.hpp"
#include "Shape.cpp"
using namespace std;
int main() {
double pi = 3.1416; // Variable for pi
// Instantiate class objects
Point guard;
Circle k;
Cylinder cid;
// Instantiate pointers to class objects
Shape *pptr;
Shape *kptr;
Shape *cptr;
// Assign memory of objects to pointer variables
pptr = &guard;
kptr = &k;
cptr = &cid;
// Call objects via pointers and print members
pptr->printShapeName();
pptr->print(5,6);
cout << '\n';
kptr->printShapeName();
kptr->print(9,pi);
cout << '\n';
cptr->printShapeName();
cptr->getHeight();
cptr->setHeight(8);
cptr->print(5,pi);
return 0;
}
Cylinder 클래스의 인쇄 기능에 세 번째 높이 인수를 추가하려고하면 오류가 발생합니다. 그것은 내 서클 클래스 정의를 사용하여 끝납니다.
"PLEASE ASSIST"여기가 중요합니다. "실린더 클래스에서 인쇄 및 볼륨 기능을 제대로 수행 할 수 없습니다." 무슨 일 이니? 귀하와 답변자가 문제가 무엇인지 알 때 대답은 항상 더 좋습니다. – user4581301
나는 이것을 강력히 추천한다 :'#include "Shape.cpp"'. 헤더 파일을 포함하십시오. 구현 파일을 컴파일하고 링크하십시오. IDE가 자신이해야 할 일을하고이 파일을 컴파일 및 링크하여 링커 오류가 발생하게 할 수 있습니다. 우리는 당신의 문제가 무엇인지 모른다. 아니면 아닐 수도 있습니다. – user4581301
OO 프로그램을 올바르게 구현하지 않습니다. 오류가 아니라 오류와 연결될 가능성이 큽니다. –