Thread: Help with basic class and object coding.

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    6

    Help with basic class and object coding.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    class Specs_M{
        
    private:
        string height;
        string weight;
    public:    
        Specs_M(string y, string z){
            setweight(y);
            setheight(z);
        }
        void setweight(string x){
            weight=x;
        }
        string getweight(){
            return weight;
        }
        void setheight (string x){
             height=x;
        }
        string getheight(){
            return height;
        }
    };
    
    
    class Specs_P {
        private:
            string age;
            string weightp;
            string heightp;
        public:
            Specs_P(string z){
                setage(z);
            }
        void setage(string x){
                age=x;
            }
        string getage(){
            return age;
        }    
        /*Specs_P(string g, string f){
            setweightp(g);
            setheightp(f);
        }*/
        void setweightp(string y){
            weightp=y;
        }    
         string getweightp(){
            return weightp;
            } 
        void setheightp(string v){
            v=heightp;
        }
        string getheightp(){
            return heightp;
        }
    };
    
    
    int main() {
        
        Specs_M object1("136 lbs","70 inches");
        cout<<object1.getweight()<<"\n"<<object1.getheight()<<endl;
        
        Specs_P object2("Age of user: unknown");
        cout<<object2.getage()<<endl;
        
        string g;
        cout<<"Enter your weight in lbs with units."<<endl;
        cin>>g;
        Specs_P object3;
        object3.setweightp(g);
        
        string f;
        cout<<"Enter your height in inches with units."<<endl;
        cin>>f;
        // Specs_P object3(g,f);
        Specs_P object4;
        object4.setheightp(f);
        
        cout<<"Your weight is:"<<object3.getweightp()<<"\n Your height is:"<<object4.getheightp()<<endl;
        
        
        return 0;
    }
    In the code above, I removed the constructors by commenting them off. Instead, I wanted to set the variables through an object. However, when I run the code, I get an error message pertaining to the Specs_P object3; and Specs_P object4;.

    The message is no matching functions for call to Specs_P::Specs_P().

    Why is this?

    Any help is appreciated. thanks

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Specs_P object3; will construct the object with the default constructor, since you don't have it's an error.
    Just add Specs_P() = default; to your Specs_P class.
    Why do you return copies in your get functions ?
    Normal practice is to return a const reference, also in your setter it's better to use a const reference.
    Example:
    Code:
    class Specs_M 
    {
    private:
      string height;
      string weight;
    public:
      Specs_M() = default;
      Specs_M(const string& y, const string& z) 
      {
        setweight(y);
        setheight(z);
      }
      void setweight(const string& x) { weight = x; }
      const string& getweight() const { return weight; }
      void setheight(const string& x) { height = x; }
      const string& getheight() const { return height; }
    };

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    6
    Ok, thank you. I haven't learned that stuff yet. Everything I've learned is from a youtube tutorial, so some of the stuff may be against common practice.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Personally I don't recommend to use YouTube. Too much old crap from people who are not experts.

    Better to use a good book.
    The C++ Programming Language Bjarne Stroustrup
    The C++ Programming Language: Amazon.co.uk: Bjarne Stroustrup: 0000321563840: Books

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help on this fstream object coding
    By MyRedz in forum C++ Programming
    Replies: 5
    Last Post: 12-26-2009, 11:26 AM
  2. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  3. open gl coding, animating an object
    By chris285 in forum Game Programming
    Replies: 1
    Last Post: 11-18-2003, 10:51 AM
  4. basic object/class question
    By Noobie in forum C++ Programming
    Replies: 8
    Last Post: 05-17-2003, 05:54 AM
  5. Replies: 3
    Last Post: 02-12-2002, 10:09 PM

Tags for this Thread