Thread: First code for Classes and Objects

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    10

    Unhappy First code for Classes and Objects

    How do I even begin this code? The general instructions given are so not newbie friendly.... =( im stuck...

    I would like to work on the code myself but I need help starting it up.....


    PLEASE DO NOT PASTE FULL CODE ON THIS MSG BOARD! THATS NOT WHAT I'M ASKING FOR.... I just need the help in getting it started.

    Thanks guys!

    ************************************




    Create a project in which you add a class called Automobile (both a .h and a .cpp file for the class definition).

    The class should have four data members (make of the auto [string], model of the college [string], the year that the auto was manufactured [int], and the purchase price of the auto [doulbe]).

    Have the main function declared a third file (the .cpp file with the same name as the project).

    Have a single constructor with all four values for the data members sent as arguments and assign these values inside the constructor. Also have get and set functions for each data member.

    Finally have a function that prints out all of the info about an object from the class. Inside the main function use the constructor two times to create two objects with all four data members sent to the constructor.

    For each object call the function with the object to output all of the information about the object. In total you should have three files: (1) the .cpp file with the main function, and (2 and 3) the two files that were created when you added a new class. In the .h file (called the header file) you should have the definitions of all of the data members.

    In addition the header for each of the member functions should be in the .h file. The actual definitions of the functions should be included the the .cpp file with the same name as the class that was created.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I can't imagine what more you could want here. You are told, explicitly, exactly what member variables (two strings, and int, and a double) your class should contain. You are told exactly what functions to write, and what they should do, and even where to call them.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Post what you've got so far for your 3 source files.

    eg.. (this code doesn't run, it's how you start it)

    Code:
    //Automobile.h
    
    #ifndef AUTOMOBILE_H
    #define AUTOMOBILE_H
    
    #include <string>
    
    class Automobile
    {
    public:
      Automobile(); //constructor 
    
    private:
      std::string model;
    };
    
    #endif
    
    
    //Automobile.cpp
    
    #include "Automobile.h"
    
    Automobile::Automobile() //constructor 
    {
    
    }
    
    
    //ProjectName.cpp
    
    #include "Automobile.h"
    
    int main()
    {
    
    }
    You should be able to do the rest, or come back and post when you're stuck.
    Last edited by Dae; 09-14-2009 at 10:51 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's too much code, Dae, IMO. It's also not quite correct.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved View Post
    That's too much code, Dae, IMO. It's also not quite correct.
    I don't think thinks so. He still needs to improve the constructor, and add the member variables and methods. I removed the usage (would have needed fixing anyway).

    It's not full code, so it can't be used, it's just a reference of how it could it look to start out. I think that's appropriate.

    Edit: By not quite correct did you mean I messed up the includes? lol fixed.
    Last edited by Dae; 09-14-2009 at 10:52 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> By not quite correct did you mean I messed up the includes?
    No, but it looks like you removed the part I was thinking of, anyway. Your variable declarations included parentheses, which made them function declarations. Not a big deal, I was just trying to put a little doubt in the OP's mind so he or she thought a little harder about what you did instead of just grabbing it and running.

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved View Post
    >> By not quite correct did you mean I messed up the includes?
    No, but it looks like you removed the part I was thinking of, anyway. Your variable declarations included parentheses, which made them function declarations. Not a big deal, I was just trying to put a little doubt in the OP's mind so he or she thought a little harder about what you did instead of just grabbing it and running.
    I'm gonna put that in my signature. People should have doubt about anything I say. Yeah I was trying to remove parts so it wasn't too much, but it's not very much code. I always hated that, in other languages I can or am forced to use default constructor (or initializer) parentheses, but cannot in C++. I actually wrote that as pseudo-code, then converted it cause I didn't want to confuse the OP (especially about inclusion guards).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Ok this is what I have so far for the header file;

    Code:
    //Automobile.h
    #include <iostream>
    using std::cout;
     
     
    
    
    #ifndef AUTOMOBILE_H
    #define AUTOMOBILE_H
     
    #include <string>
    using std::string;
     
    //Automobile class defenition
    class Automobile
    {
    public:
    Automobile(); //constructor 
    
    
    private:
    std::string model;
    std::string make;
    };

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CrKrJak
    Ok this is what I have so far for the header file;
    Okay, so follow your instructions step by step. The instructions mention four data members, but at the moment you only have two. The instructions mention getter and setters for each data members, but at the moment you have none. The instructions state that you should have "a single constructor with all four values for the data members sent as arguments", but at the moment you have a default constructor instead.

    Also, your header inclusion guards should start the very top of the header file, and you are missing the #endif. You do not need to #include <iostream> at this point, so do not so do. Oh, and avoid using declarations, except within a restricted scope, in header files, i.e., remove the using std::cout and using std::string.

    You should indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Retrieving the exit code of CWinThread objects
    By mynickmynick in forum Windows Programming
    Replies: 4
    Last Post: 10-07-2008, 08:10 AM
  2. Choosing a compiler
    By pushingsquares in forum Game Programming
    Replies: 27
    Last Post: 12-14-2006, 10:33 AM
  3. The right way to code.
    By g4j31a5 in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2006, 01:58 AM
  4. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  5. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM