OK so I'm doing this assignment and i'm confuzzled:
write a basic class, with a constructor, default constructor, setter and getter functions. This is what I got:

class Car {

      private:
               int yearModel;
               string make;
               int mpg;
      public:
               Car(int, string, int);                     //constructor
               void setDefault(int, string, int);         // def constructor prototype
};

void car::setDefault (int a = 0; string ab = ""; int b = 30){ //def constructor definition
       yearModel=a;
       mpg=b;
       make=ab;
}

Is that correct? How are getter and setter functions done? I've read the chapter twice, looked online and stumped HEEEEEEEEEEEEEEELLLLLLLLLLPPPPPPPPPP

Recommended Answers

All 7 Replies

Try this to start out...

#include <string>

using namespace std;

class car 
{

      private:
               int yearModel;
               string make;
               int mpg;
      public:
               car();                  
};

car::car ()
{
       yearModel = 0;//zero is a good value to set numeric primary data types
       mpg = 0;//zero is a good value to set numeric primary data types
       //make string object will call its default constructor automatically 
}

int main()
{
	car mycar;
	return 0;
}
class Car {
       private:
               int yearModel;
               string make;
               int mpg;
      public:
               Car(int, string,int);       //constructor #1
               Car(int, int, string);      //another constructor #2 
               Car(string, int, int);      //another constructor #3
               Car();         // def constructor prototype #4
};

ALL constructors take on the name of the class. You can have 20 constructors for a single class if you want as long as the paramters used in calling it are different. Based off the parameters, it will determine which one to use.

A constructor with NO parameters is the default constructor. So for instance

Car *a = new Car(1, "Bob", 10); //uses #1
Car *b = new Car(1, 10, "Bob"); //uses #2 
Car *c = new Car("Bob", 1, 10); //uses #3
Car *d = new Car(); //uses #4 and calls the default constructor

Hope this helps.

A constructor with NO parameters is the default constructor. So for instance

Car a = new Car(1, "Bob", 10); //uses #1
Car b = new Car(1, 10, "Bob"); //uses #2 
Car c = new Car("Bob", 1, 10); //uses #3
Car d = new Car(); //uses #4 and calls the default constructor

Hope this helps.

Don't you mean Car *a = new Car()...

Don't you mean Car *a = new Car()...

Yes, thanks

A constructor with NO parameters is the default constructor. So for instanc

Hope this helps.

1. Provide a default constructor that assigns 0 to the yearModel and mpg, and the empty string to the make.

1. Provide a default constructor that assigns 0 to the yearModel and mpg, and the empty string to the make.

gerard did the yearModel and mpg in his post. All you need to add into that is

make = "";

ok i think i got it. thanks every1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.