Moi, aloittelen jälleen c++ ohjelmointia ja en saa ratkaisua toimimaan vaikka on mielestäni suoraan oppaan esimerkin mukainen, koodi:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Shape
{
protected:
int width, height ;
public:
Shape( int w, int h ) { width = w; height = h; }
virtual int area() const = 0; // const member: the function is prohibited from modifying the object
};
//oma osuus alkaa
class Rectangle : public Shape
{
public:
int area();
Rectangle(int w, int h);
};
Rectangle::Rectangle(int w, int h) : Shape(w,h){}
int Rectangle::area(){return width*height;}
class Triangle : public Shape
{
public:
int area();
Triangle(int w, int h);
};
Triangle::Triangle(int w, int h) : Shape(w,h){}
int Triangle::area(){return width*height*0.5;}//int palautustyyppi vaikka 0.5 kerroin, ei takerruta nyt siihen kiitos
//oma osuus loppuu
int main(int argc, char *argv[])
{
// The generation of random integers a and b is hidden
Rectangle rect(a, b);
Triangle trgl(a, b);
cout << "a = " << a << " and " << "b = " << b << endl;
cout << "Rectangle area: " << rect.area() << endl;
cout << "Triangle area: " << trgl.area() << endl;
return 0;
}virhe:
Cannot declare variable "rect" to be of abstract type "Rectangle"
because virtual int Shape::area() const virtual fuction is pure within "Rectangle"
Tehty hassun hauskan c++-oppaan neuvoin http://www.nic.funet.fi/c opas/periytym.html
Kyseessä koulutehtävä ja oman koodin osuus merkitty kahdella kommentilla
EDIT: keskustelun otsikon kirjoitusvirhettä ei pysty editoimaan pois :(
Shape:ssa on määritelty virtual int area() const, mutta muissa int area()
Et siis toteuta omissa luokissasi Shapen vaatimaa area-metodia. Ongelma korjaantuu lisäämällä const-sana metodien perään, kuten Shapessakin.
Selvä, kiitos avusta!
Aihe on jo aika vanha, joten et voi enää vastata siihen.