-3
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

class shape
{
public:
    int width;
    int height;
    data(int w,int h){width=w;height=h;}
};

class Rectangle: public Shape
{
  public:
    int area ()
    {
        return (width * height);
    }
};


class Triangle: public Shape
{
public:
    int area ()
    {
        return (width * height / 2);
    }
};
int main()
{
    Rectangle rect;
    Triangle tri;
    rect.data(5,3);
    tri.data(10,2);
    cout<<rect.area()<<endl;
    cout<<tri.area()<<endl;
    return 0;
}

 
  • my code did not achive the inherting part and i am gitting erorr masseage that says "error: expected class-name before '{' token| "

  • the erorr in line ( class Rectangle: public Shape )

  • so what can i do to solve this?

Flir0o
  • 11
  • 2
  • 4
    `shape` is not the same as `Shape`. – Bert Aug 02 '20 at 16:03
  • 1
    The code shown doesn't define a class named `Shape`, so there's nothing to derive from. – Igor Tandetnik Aug 02 '20 at 16:04
  • 4
    Please read [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) and `using namespace std;` on top just makes a bad thing into a catastrophe, see [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058). Those to together is a recipe for a lot of pain (and either one on its own is also painful - first one definitely the worst of the two). – Jesper Juhl Aug 02 '20 at 16:07
  • `class Shape` -> `class Shape`. And `data(int w,int h){width=w;height=h;}` probably should be a constructor `Shape(int w,int h){width=w;height=h;}`? – churill Aug 02 '20 at 16:07
  • @churill I believe you meant to say `class shape` -> `class Shape` (and I agree), but in your comment you used `Shape` in both cases. – Jesper Juhl Aug 02 '20 at 16:10
  • thanks it solved sorry i was not soucsed – Flir0o Aug 02 '20 at 16:14
  • @JesperJuhl Yes, my bad, too late to edit. – churill Aug 02 '20 at 16:16

1 Answers1

0

There are several errors:

  1. The C++ is case-sensitive, shape isn't equivalent to Shape identifier. One of them must be the same as the other.

  2. The class variables must be private, protected when the base class is to be derived. Otherwise, it violates the rule of OOP.

  3. The datatype of data() defaults to int but nothing's returned. That's why not getting print anything. Change it to void.


Thus, the correct class code should look something like (notice comments):

class shape {

// The identifiers followed by 'protected:' will be visible to the class only
protected:
    int width;
    int height;

public:
    // Must be 'void' since it's returning nothing and defaults to 'int'
    void data(int w, int h) {
        width = w, height = h;
    }
};

// 'shape' != 'Shape'
class Rectangle : public shape {
public:
    int area(void) {
        return (width * height);
    }
};

// 'shape' != 'Shape'
class Triangle : public shape
{
public:
    int area(void) {
        return (width * height / 2);
    }
};
Rohan Bari
  • 6,523
  • 3
  • 9
  • 30