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

  • SEARCH
  • Home
  • 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 8777495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:17:31+00:00 2026-06-13T19:17:31+00:00

I got a parent class call Shape Shape got 2 child call Square and

  • 0

I got a parent class call

Shape

Shape got 2 child call

Square and Rectangle

Shape class got a variable call area, which is of int type

So i created some object of Square, Rectangle like this

int main()
{
    Shape *shaped[100];

    //then i did some adding of object..
    int areaValue;
    areaValue=1;

    shaped[0] = new Rectangle();
    shaped[0]->setArea(areaValue);

    areaValue=7;
    shaped[1] = new Square();
    shaped[1]->setArea(areaValue);

    areaValue=5;
    shaped[2] = new Square();
    shaped[2]->setArea(areaValue);

    shapeCounter = 3;

    sort(shaped[0],shaped[2]);

    for (int i=0;i<shapeCounter;i++)
    {
        cout << shaped[i].getArea() << endl;
    }

}

I try to sort by ascending area but it doesnt work. no position change, the area still in the same sequence.

Thanks for all help!

Update:

I did the following changes at Shape.cpp

 bool Shape::orderByArea(const Shape* lhs, const shape* rhs)
    {
      return lhs->area() < rhs->area();
    }

Then at main.cpp I did this

std::sort(shaped, shaped + 3, orderByArea);

however i get an error, orderByArea was not declared in this scope.

Another thing i tried was:
To sort using vector

At Shape.h

public:

bool operator<const Shape& x) const
{
return area < x.area;
}

At main.cpp

vector<ShapeTwoD*> sortVector;
sortVector.clear();
sortVector.assign(shaped,shaped + shapeCounter);

sort(sortVector.begin(),sortVector.end());

for(int i=0;i<shapeCounter;i++)
{
cout << sortVector[i].toDisplay() << endl;
}

But nothing seems sorted. I try do a printout its position is same.

Updates: Its fixed now. sort is working . Thanks to the experts !

I got another question is

Shape *shaped[100];

How do i copy the value of

Shape *shaped[100];

into

vector<Shape> myVector;

instead of

vector<Shape*> myVector;

so i can use the normal object sort.

  • 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-13T19:17:32+00:00Added an answer on June 13, 2026 at 7:17 pm

    In your code how did you expect the compiler to know that you wanted to sort by area, magic? I would recommend reading a book on the standard C++ library (aka the STL), it will explain how to do custom sorting. In your code you have an array of pointers, so you should write a functor that can order your pointers. Also your parameters to std::sort are wrong. Your array starts at shaped, and ends at shaped + 3 (since you have three elements in your array).

    struct sort_by_area
    {
        static bool operator()(Shape* x, Shape* y)
        {
            return x->getArea() < y->getArea();
        }
    };
    
    sort(shaped, shaped + 3, sort_by_area());
    

    Untested code, apologies for any mistakes.

    Or you can use a function pointer as juanchopanza says.

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

Sidebar

Related Questions

I have this Parent Class call Shape (ShapeTwoD.cpp) He got 2 Child Class 1
Got some error in codeigniter when trying to call parent class. This is what
Here is the issue with my code I got a parent class Shape and
I've got a usercontrol which should call a Sub of the parent page when
I got a scenario like this Class Parent { Property A; } Class A
I've got Parent - Child relationship OneToOne, but two of them. Annotations are not
Traditional wisdom says not to call into child namespaces from a parent. Let's say
I'm new to OO PHP. Got some questions. class a { protected function a1()
I've got a class called Request . At some point in that class I
I've got a class that looks something like this: public class Parent { public

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.