Тема: Наслідування класів!
Ось роблю завдання і така проблема!
Сама суть наслідування працює, але при обчисленні значень виводить нереальний результат!.
#include<iostream>
#include "Shape3D.h"
#include "Sphere.h"
#include "Cube.h"
using namespace std;
void main(void)
{
    setlocale(0,"");
    float a,b,c; int i;
    while(1)
    {
        cout<<"\n МЕНЮ: \n1-Куб\n2-Сфера\n3-Вихiд:\n";
        cin >> i;
        switch(i)
        {
        case 1:
            {
                cout << "Куб:\n";
                cout << "Введiть довжину ребра куба:\n";
                cin >> a;
                Cube *tri;
                tri =new Cube(a);
                cout<<"Площа:" << tri->Area()<<"\n";
                cout<<"Об'єм:"<<tri->Volume()<<"\n";
                tri->PrintMessage();
                delete[] tri;
                break;
            }
        case 2:
            {
                cout<< "Сфера:\n";
                cout << "Введiть радiус сфери:\n";
                cin >> a;
                Sphere *rec;
                rec =new Sphere(a);
                cout<< "\n"<<"Площа поверхні:" << rec->Area();
                cout<< "\n"<< "Об'єм:"<<rec->Volume()<<"\n";
                rec->PrintMessage();
                delete [] rec;
                break;
            }
        case 3:
            {
                return;
            }
        }
    }
}
#ifndef SHAPE3D_H
#define SHAPE3D_H
#include <iostream>
using namespace std;
class Shape3D
{
public:
    Shape3D();
    virtual ~Shape3D();
    virtual float Area()=0;
    virtual float Volume()=0;
    virtual void PrintMessage();
};
#endif
#include"Shape3D.h"
#include<iostream>
using namespace std;
Shape3D::Shape3D()
{
    cout<<"Constructor 3D Shape \n";
}
Shape3D::~Shape3D()
{
    cout<<"Destructor  3D Shape \n";
}
void Shape3D::PrintMessage()
{
    cout<<" This is 3D Shape";
}
#ifndef SPHERE_H
#define SPHERE_H
#include <iostream>
#include "shape3d.h"
class Sphere  : public Shape3D
{
public:
    Sphere();
    ~Sphere();
    Sphere(float rs);
    float Volume();
    float Area();
    void PrintMessage();
private:
    float r;
};
#endif
#include "Sphere.h"
using namespace std;
Sphere::Sphere()
{
    r=0;
}
Sphere::Sphere(float r)
{
    float rs=r;
}
Sphere::~Sphere()
{
}
float Sphere::Volume()
{
    float pi=3.14;
    return (4/3)*pi*r*r*r;
}
float Sphere::Area()
{
    float pi=3.14;
    return 4*pi*r*r;
}
void Sphere::PrintMessage()
{
    cout << "Shape type is  Sphere \n";
}
#ifndef CUBE_H
#define CUBE_H
#include <iostream> 
#include "shape3d.h"
class Cube :public Shape3D
{
public:
    Cube();
    ~Cube();
    Cube(float st); 
    float Volume();
    float Area();
    void PrintMessage();
private:
    float s;
};
#endif
#include "Cube.h"
#include <math.h>
using namespace std;
Cube::Cube()
{
    s=0;
}
Cube::Cube(float st)
{
    s=st;
}
Cube::~Cube()
{
    cout<<"Destructor  Cube \n";
}
float Cube::Volume()
{
    return s*s*s;
}
float Cube::Area()
{
    return 6*s*s;
}
void Cube::PrintMessage()
{
    cout << "Shape type is Cube";
}
Без назви-2 копія.jpg 73.02 kb, 218 downloads since 2013-04-05