class error?think?

This is a discussion on class error?think? within the C++ Programming forums, part of the General Programming Boards category; Code: class color { public: void getColor(); int setColor(); private: int color; }; void Color::getColor(char color) { this->color=color; } int ...

  1. #1
    *s*T*i*X*
    Join Date
    Jul 2005
    Location
    LA
    Posts
    2

    Question class error?think?

    Code:
    class color {
        public:
               void getColor();
               int setColor();
        private:
                int color;
    };
    
    void Color::getColor(char color)
    {
         this->color=color;
    }
    
    int Color::setColor()
    {
        return color;
    }
    
    
    int main()
    {
        char color;
        
        cout<<"The color now, is currently"<<color.setColor()<<".";
        cout<<"\n\nWe will now set the color to"<<color.getColor()<<".";
        cout<<"\n\nChoices appended below.";
        color.getColor(blue);
        color.setColor(blue);
        cout<<"\n\nYour color is now"<<color.setColor()<<".";
        cout<<"\n\nYour color was set to"<<color.getColor()<<".";
        system("pause");
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Yes, you have errors. Lots of them. And they're all easy to avoid if you pay attention when writing the code. Throw away your program and do it again.
    My best code is written with the delete key.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    here an example of a class


    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class dog
    {
    	public:
    	void feeddog(string);
    	void kickdog(int);
    }dogs;
    
    void dog::feeddog(string food)
    {
    	cout << "You fed the dog : " << food << endl;
    }
    void dog::kickdog(int pounds)
    {
    	cout << "You kicked the dog with " << pounds << " pounds of force!" << endl;
    }
    
    int main()
    {
    	dogs.feeddog("poop");
    	dogs.kickdog(10);
    	cin.get();
    	return 0;
    }

    this is a real simple class, compare it to your and see why you
    have errors.

  4. #4
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    class Color
    {
    public:
           // const is used becouse this function
           // does not change anything. It just returns color.
           string getColor() const;
           
           // void? Function just sets the color.
           // Does not return anything.
           void   setColor(string new_color);
    
    private:
    
            string color;
    };
    
    string Color::getColor() const
    {
        // return member of the Color class, 'color'
        return Color::color;
    }
    
    void Color::setColor(string new_color)
    {
        Color::color = new_color;
    }
    
    
    int main()
    {
        Color myColorer;   // make instance of the Color class
    
        cout<<"The color now, is currently "<<myColorer.getColor()<<" .";
        cout<<"\n\nWe will now set the color to black.";
        myColorer.setColor("black");
    
        cout << "\nNow the color is " << myColorer.getColor();
    
        getchar();     // wait for keypress
    }
    [NOTE] :
    Some things won't work if you have older compiler.

    Code:
     
     .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW= 
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i think you have an old compiler, getchar()?
    (at least i think old conio.h use to have this
    or getch() or something.)

    cin.get();


    would be the (standard)choice around here.

  6. #6
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    Same like you... VS.NET 2003 Arch.
    All those years I use getchar() .
    It is little stupid to include conio.h just for getch() .

    ...will try to use cin.get() from now... if it is some standard...
    Code:
     
     .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW= 
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  7. #7
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    Hitachi,
    There are some nice tutorials on this site about classes and OOP.
    You could read this tutorial : http://www.cprogramming.com/tutorial/lesson12.html
    Sample code is well documented.

    ...and remember, Google is your friend -> http://www.google.com/search?num=30&...Tra%C5%BEi&lr=

    C++ by example might be good book for you -> ftp://194.105.193.56/pub/warez/books...by-example.zip


    And more advanced book -> http://www.gnacademy.org/text/cc/Tutorial/tutorial.html .
    Use it as reference.
    Last edited by 81N4RY_DR460N; 07-30-2005 at 10:20 PM.
    Code:
     
     .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW= 
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >All those years I use getchar()
    Then you were relying on a non-portable feature, because getchar() requires that you include <cstdio>.

    >...will try to use cin.get() from now... if it is some standard...
    Yes, it is. Just like getchar().
    My best code is written with the delete key.

  9. #9
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    @Prelude
    cstdio ?
    Never used this... Maybe VS.NET automatically inserts this...

    For me, it's not important if something is portable.
    Maybe i will be Windows-only developer (becouse of direct X).
    Don't know too much about OpenGL...
    Does it support Shader model 3.0, FSAA, Anisotropic filtering(and how much), AA(#), Bump mapping, and all those cutting edge eye candy things....?
    I've played IL 2 Sturmovik - Forgotten battles. It uses OpenGL, and graphics was very good.
    Can someone refer me to some site where can I find answer on those questions? I'll see on the OpenGL's homepage.
    BTW, if OpenGL doesn't support Shaders 3.0, can usermade library add that functionality to Graphics Engines?
    Sorry for all that off-topic.
    Code:
     
     .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW= 
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  10. #10
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i could be wrong but portable, doesnt just mean to another
    operating system but to another compiler. so in the future
    if you upgrade your operating system, or that compiler just
    becomes to old, or you just find a new compiler that you
    want to start using. all your previous code that you used
    getchar() may not compile without changes.


    its always good to be worried about those things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21