Thread: help in object

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    help in object

    i have this program and i cant compile it

    Code:
    #include<stdio.h>
    #include<iostream>
    #include<conio.h>
    
    class dog
    {
          int age, weight;
          char color;
         
          public:
                void setvalues(int, int, char);
    };
    
    void dog::setvalues(int age, int weight, char color)
    {
         this -> age = age; // using this-> will explicily refer to the class member
          weight -> = weight;
          color -> = color;
    
    };
    main()
    {
          dog fido; 
          fido.setvalues(12,55,"gray");
          
          getch();
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    At first a lot of sintax errors!!

    Code:
    #include<stdio.h>
    #include<iostream>
    #include<conio.h>
    
    class dog
    {
          int age, weight;
          char color;
         
          public:
                void setvalues(int, int, char);
    };
    
    void dog::setvalues(int age, int weight, char color)
    {
         this -> age = age; // using this-> will explicily refer to the class member
     ??     weight -> = weight; 
     this->weight = weight; 
     // see before     color -> = color;
    
    };
    main()
    {
          dog fido; 
          fido.setvalues(12,55,"gray");
          
          getch();
    
    }
    2) setvalues if for a char data and not for a *char data.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include<iostream>
    #include <string>
    
    class dog
    {
          int m_age, m_weight;
          std::string m_color;
         
          public:
                void setvalues(int age, int weight, const std::string& color);
    };
    
    void dog::setvalues(int age, int weight, const std::string& color)
    {
         m_age = age; // using this-> will explicily refer to the class member
          m_weight = weight;
          m_color = color;
    
    }
    
    int main()
    {
          dog fido; 
          fido.setvalues(12,55,"gray");
    }
    I have fixed the code to make it compile.
    I will explain a few things. Others can fill in the rest.
    char is a character, double quotes indicates a string. A string is of type std::string.
    You don't need to use "this->" to refer to member. That is, if your members are of different names than your parameters.
    weight -> makes no sense.
    main must have a return type. That type shall be int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Interview questions, please help
    By higaea in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2010, 06:35 AM
  2. Inheriting Linked List
    By robolee in forum C++ Programming
    Replies: 20
    Last Post: 10-17-2009, 07:50 PM
  3. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM