Need a little help

Write a program that creates a Bus class. Create a constructor that initializes the number of passengers and number of seats. Declare four objects. Use the default copy constructor to initialize two of the objects.

Here's what I have, what did I do wrong?
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<deque>

using namespace std;

class bus
{
int num_seat;
int num_passenger;

public:
bus()
{
num_seat = 0;
num_passenger = 0;
}

bus( int a, int b)
{
num_seat = a;
num_passenger = b;
}

void print()
{
cout<<"number of seats"<<num_seat<<endl;
cout<<"number of passengers "<<num_passenger<<endl<<endl;
}
};

int main()
{
bus obj1,obj2,obj3,obj4;
bus obj,obj_user(200, 150);

cout<<"object 1 is being initialised using default copy constructer\n\n";
obj1 = obj;
obj1.print();
cout<<"object 2 is being initialised using user copy constructer\n\n";
obj2 = obj_user;
obj2.print();
return 0;
}
You've added more that what has been asked for.

You have not been asked to provide a default constructor (one that doesn't require arguments).

You've been asked to create four objects, not six.

You're on the right track with obj_user. You just need one more like that and two that are copies.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>

using namespace std;

class bus
{
public:
    bus( int a = 7, int b = 25) // 7 and 25 are default values
    {
        num_seat = a;
        num_passenger = b;
    }

    void print()
    {
        cout<<"number of seats "<<num_seat<<endl;
        cout<<"number of passengers "<<num_passenger<<endl<<endl;
    }

private:
    int num_seat;
    int num_passenger;
};

int main()
{
    bus obj;// constructed using default values
    
    bus obj1 = obj;
    cout << "object obj1 is being constructed using default copier\n";
    obj1.print();

    cout << "object obj-user constructed using class constructor with user parameters and then copied\n";
    bus obj_user(200, 150);
    bus obj2 = obj_user;
    obj2.print();

    return 0;
}


This might be a bit clearer. The default copying (of properties) is done by the compiler, not by your program.
Topic archived. No new replies allowed.