Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:57:51+00:00 2026-06-11T08:57:51+00:00

So I have these header files: Shape.h: class Shape { public: Shape(); virtual ~Shape();

  • 0

So I have these header files:

Shape.h:

class Shape
{
public:
    Shape();
    virtual ~Shape();
    virtual double getArea();
    virtual void printDraw();
    bool isLegal(Shape shape);
};

Trig.h:

class Trig : public Shape
{
public:
    Trig(Point pointA, Point pointB, Point pointC);
    virtual ~Trig();
    virtual double getArea();//override
    virtual void printDraw();//override
    bool isLegal(Trig trig);//override
};

but when I try to implement Trig.cpp, I get errors.
I tried :

Trig::Trig(Point pointA, Point pointB, Point pointC) {..}

I’ve looked through the internet, but I don’t seem to do the inheritance properly.
[My code MUST have header file that includes the declarations only!… Requirements of the assignment!]

I’m new in using both inheritance and virtual functions in C++ [I’ve used inheritance in Java]

And concerning the virtual functions.. is there any difference in their implementation than in normal functions ? [I understand the difference in the behavior].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:57:53+00:00Added an answer on June 11, 2026 at 8:57 am

    In a comment you explain that

    “i get the error: undefined reference to `Shape::Shape()’”

    That means that you have forgotten to provide an implementation of that constructor.

    Now, with the technical problem fixed, look at the design.

    The first two methods suffer from two maladies: (1) although they should never mutate an object they are not declared const, so they cannot be called on a const object, and (2) the get prefix reduces readability, is more to type and has no general advantage. I’ve found a get prefix useful in some rare circumstances as a disambiguation device, but all usages I’ve seen by beginners have been inappropriate copying of a Java convention, which makes sense in Java but not in C++. So, instead of …

    virtual double getArea();
    virtual void printDraw();
    

    do

    virtual double area() const;
    virtual void print() const;
    

    Then, the method …

    bool isLegal(Shape shape);
    

    is wrong in so many ways… Ouch! But let’s start with the purely technical.

    From a purely technical point of view it is needlessly inefficient to pass the argument by value, which incurs a copy operation for each call. Instead pass the object by reference. And make that a reference to const, in order to support const objects and rvalue objects as arguments:

    bool isLegal(Shape const& shape);
    

    Next, naming: isLegal is an ungood name because most any C++ object will be legal. It would have to be, say, pornographic and residing in a non-Western country, in order to become illegal. And I can’t for the life of me think of any way to make an object pornographic, or make it reside in some specific geographic region.

    So,

    bool isValid(Shape const& shape);
    

    Next, low level design, there is no good reason to have that non-virtual method as an ordinary member function, because that requires that you call it on an object. But all the information it needs is in the ordinary argument. We can see this confusion in the derived class, where …

    bool isLegal(Trig trig);//override
    

    is not at all an override: it’s technically an overload of the function name, which means, just a different function with the same name. There’s no virtuality here, no override. And it is not needed.

    So, make that a static member function, which does not have to be called on an object:

    static bool isValid(Shape const& shape);
    

    Finally, higher level design, the whole machinery of C++ constructors and destructors is there in order to avoid such methods and checking.

    The idea is that you …

    • Establish a valid object in every constructor.

    • Keep the object valid in every method.

    Then the object simply can’t become invalid. This approach is called single phase construction, and the properties of the object that makes it “valid’ are known as the class’ class invariant. Which should be established by every constructor, and maintained by every method.

    This means, final version, the isValid function IS REMOVED: it has no job to do, because that job is (properly) done by the constructor(s) and the methods.

    Okay, there are some technical challenges with single phase construction, in particular how to do derived class specific initialization in a base class constructor. This is covered by the C++ FAQ. It’s often a good idea to read the FAQ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these simplified types: public class DataBean extends ZZZZZ { public String name;
I have these two header files and one produces an error if I don't
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
I have a bunch of static html files that share same header and footer.
I have three big header files (4.5MB and more), with huge static arrays inside.
I have a question about the relationship between header and implementation files in C++.
I'm facing difficulty in understanding source and header files stuff. Suppose 1)I have a
Where can I find these header files on Windows 7? I just installed VS2010,
I have a header file buildTree.h and a C file buildTree.c there is a
Details on fonts: When you have some text in specific font, there's a header

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.