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 8758917
In Process

The Archive Base Latest Questions

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

C++ How to create subclass in class with array Hi guys, i still learning

  • 0

C++ How to create subclass in class with array

Hi guys, i still learning C++ and face some issues here.

Basically i got a parent class

lets call this parent class as

Vehicle

It got 2 sub class, lets assume they are

Car and Motorcycle

I will create a vehicle object assume vehicle size is 20

Vehicle veh[20]

I will do the following

string vType;

cout << "Please enter your vehicle Type:";
cin >> vType;

so i do a comparision if (vType==”Car”)

it will return 4 wheels from the sub class, but how do i declare its 4 wheels at Car and 2 wheels at Motorcycle , i know i need create 2 additional cpp file which is

class Car : public Vehicle
{
private:
int noOfWheels;
public:
computePrice();
}

But how do i set noOfWheels specially to Car as 4 and Motorcycle as 2.

The next is the tricky part.. after knowing how many wheel it is

i will need store a array for each wheel

string wheel[4];

since i know there 4 wheel in cars.

How do i prompt 4 type and store it in an array, and all of this in an object call Vehicle.

I can use a for loop and thats not the issue, the part i am stuck on is how do i create a string array and store the 4 prompt and then into this Vehicle[0]

wheel 1:
wheel 2:
wheel 3:
wheel 4:

When user want to print data it will be

Vehicle[0]
Type: Car
Wheel: 4
Wheel[0] = Fine condition
Wheel[1] = Need to check again
Wheel[2] = Fine condition
Wheel[3] = Might need get repair

Thanks for all help.

  • 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-13T14:40:18+00:00Added an answer on June 13, 2026 at 2:40 pm

    Firstly the declaration for your array is wrong. Since you are dealing with polymorphic classes you need to use pointers.

    Vehicle* veh[20];
    

    Otherwise you will have what is called object slicing. Which means that even if you create a Car or a Motorcycle they will be converted into Vehicles when you assign them to your array.

    ‘how do i set noOfWheels specially to Car as 4 and Motorcycle as 2.’

    In the constructor

    class Car : public Vehicle
    {
    public:
        Car() : noOfWheels(4) { ... }
    private:
        int noOfWheels;
        ...
    };
    
    class Motorcycle : public Vehicle
    {
    public:
        Motorcycle() : noOfWheels(2) { ... }
    private:
        int noOfWheels;
        ...
    };
    

    But personally I don’t think you need a noOfWheels data member at all. Since the number of wheels is fixed for each type of Vehicle it’s a waste of space, instead you need a virtual function

    class  Vehicle
    {
    public:
        virtual int noOfWheels() const = 0;
        ...
    };
    
    class Car : public Vehicle
    {
    public:
        virtual int noOfWheels() const { return 4; }
        ...
    };
    
    class Motorcycle  : public Vehicle
    {
    public:
        virtual int noOfWheels() const { return 2; }
        ...
    };
    

    ‘how do i create a string array and store the 4 prompt and then into this Vehicle[0]’

    Again I would use the constructor to initialize the car wheel names.

    class Car : public Vehicle
    {
    public:
        Car(const std::string* w)
        { wheel[0] = w[0]; wheel[1] = w[1]; wheel[2] = w[2]; wheel[3] = w[3]; }
        virtual int noOfWheels() const { return 4; }
    private:
        std::string wheel[4];
        ...
    };
    

    Use constructors to initialize classes. That’s what they are for.

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

Sidebar

Related Questions

i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
According to this answer I want to create my own subclass of Array QArray
I create a custom db.Property subclass: class PropertyFoo(db.StringProperty): def validate(self, value): # Yes this
I want to create a subclass, ess say, to the built-in ss class. I'd
In the CoffeeScript program below, I create a subclass of Array which sets two
I have a Zend_Form subclass. Some elements are set to belong to arrays. class
I have created a subclass of UITableViewController that is used as the custom class
I have a UIButton that I create in my sub class ViewController, and add
Is it possible to create subclass proxies in spring.net where the proxy is the
I'm trying to create a subclass of JButton or AbstractButton that would call specified

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.