Thread: How to learn Object Oriented Programming / Classes?

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,446
    Even if the program runs,
    Well does it compile and run or not?

    If you get compile errors or warnings post those messages exactly as they appear in your development environment.

    Why did you add void print() to your class definition? Did you implement this member function?


    Jim

    PS. I suggest you get rid of the pointers now, why wait? IMO, they're just going to confuse the issues.
    Last edited by jimblumberg; 05-26-2017 at 12:18 PM.

  2. #17
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Well does it compile and run or not?

    If you get compile errors or warnings post those messages exactly as they appear in your development environment.

    Why did you add void print() to your class definition? Did you implement this member function?


    Jim

    PS. I suggest you get rid of the pointers now, why wait? IMO, they're just going to confuse the issues.
    With the same code I put the last time:

    C++\Ejercicio 25 - Clases 2\main.cpp||In function 'int main()':|

    C++\Ejercicio 25 - Clases 2\main.cpp|21|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|

    obj\Debug\main.o||In function `main':|
    C++\Ejercicio 25 - Clases 2\main.cpp|23|undefined reference to `Furniture:: print()'|

    C++\Ejercicio 25 - Clases 2\main.cpp|26|undefined reference to `Furniture:: print()'|

    C++\Ejercicio 25 - Clases 2\main.cpp|29|undefined reference to `Furniture:: print()'|

    C++\Ejercicio 25 - Clases 2\main.cpp|32|undefined reference to `Furniture:: print()'|

    ||error: ld returned 1 exit status|

    Edited: I added a space in Furniture:: print() because if I put : p together we have a similing face.
    Last edited by JorgeChemE; 05-26-2017 at 12:38 PM.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,446
    C++\Ejercicio 25 - Clases 2\main.cpp|21|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
    The answer to this problem is to use std::strings instead of C-strings.

    C++\Ejercicio 25 - Clases 2\main.cpp|23|undefined reference to `Furniture:: print()'|
    The answer to this is that you haven't implemented this function.

    You really should consider starting simpler and compile earlier and more often. By compiling early and often you will have better ideas as to where the problems are located because the amount of modified code will be much smaller.

    You should also start using modern C++. I'd start with something more like:
    Code:
    #ifndef FURNITURE_H
    #define FURNITURE_H
    
    #include <string>
    
    class Furniture
    {
        public:
            Furniture() = default;
    
            std::string get_name() const;
            void set_name(const std::string& val);
            double compute_volume();
            void print();
    
        private:
            std::string name;  
            double length = 2.0;
            double width = 2.0;
            double depth = 2.0;
    };
    
    
    #endif // FURNITURE_H
    
    #include <iostream>
    
    //#include "Furniture.h"
    
    
    using namespace std;
    
    
    int main()
    {
        Furniture j1;
        j1.set_name("Furniture");
        std::cout << "The name of the item is: " << j1.get_name() << std::endl;
    
        j1.print();
    /*
        j1.Setvolume_furniture(volume_furniture);
        j1.print();
    
    
        j1.Setlength(10);
        j1.print();
    
    
        j1.Setwidth(20);
        j1.print();
    
    
        j1.Setdepth(30);
        j1.print();
    */
        return 0;
    }
    
    
    #include <string>
    #include <iomanip>
    //#include <Furniture.h>
    
    std::string Furniture::get_name() const
    {
        return name;
    }
    
    void Furniture::set_name(const std::string& item_name)
    {
        name = item_name;
    }
    
    double Furniture::compute_volume()
    {
        return depth * width * length;
    }
    
    void Furniture::print()
    {
        cout << setprecision(2) << fixed;
        cout << "\nPrint using a function\n" << "Furniture:  " << name;
        cout << "\nVolume of furniture:  " << compute_volume();
        cout << "\nLength: "  << length;
        cout << "\nWidth: " << width;
        cout << "\nDepth: " << depth;
    }
    Jim

  4. #19
    Registered User
    Join Date
    May 2017
    Posts
    101
    Nooooo, I was writing to you but I signed out automatically while submitting the reply. Hopefully I loaded the auto-restore:


    Jim, I finally solved the problems. I put the function print() in Furniture.cpp and I also had some errors in int main() because I printed the result 4 times and float Getvolume_furniture(); should be put below j1->Setlength(10); j1->Setwidth(20); and j1->Setdepth(30);


    I put the code, still I didn't fixed the pointers but the program works, run:


    First file: main.cpp (here I removed several j1->print() and moved j1->Setvolume_furniture();


    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Furniture.h"
    
    
    using namespace std;
    
    
    int main()
    {
        Furniture *j1 = new Furniture();
        j1->Setfurniture("Furniture");
        j1->Setlength(10);
        j1->Setwidth(20);
        j1->Setdepth(30);
        j1->Setvolume_furniture();
    
    
        j1->print();
    
    
        delete j1;
    
    
        return 0;
    }

    Second File: Furniture.h (I didn't touch anything here this time)


    Code:
    #ifndef FURNITURE_H
    #define FURNITURE_H
    
    
    class Furniture
    {
        public:
            Furniture();
            Furniture(char* furniture, float volume_furniture, float length, float width, float depth);
            virtual ~Furniture();
    
    
            char* Getfurniture();
            void Setfurniture(char* val);
            float Getvolume_furniture();
            void Setvolume_furniture();
            float Getlength();
            void Setlength(float val);
            float Getwidth();
            void Setwidth(float val);
            float Getdepth();
            void Setdepth(float val);
    
    
            void print();
    
    
        protected:
    
    
        private:
            char* furniture;
            float volume_furniture;
            float length;
            float width;
            float depth;
    };
    
    
    
    
    #endif // FURNITURE_H

    Third File: Furniture.cpp (here I added the function print to "add properties of the method to the console", words of my instructor)


    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "Furniture.h"
    
    
    Furniture::Furniture()
    {
        this->furniture=NULL;
        this->volume_furniture=0.0;
        this->length=1.0;
        this->width=1.0;
        this->depth=1.0;
    }
    Furniture::Furniture(char* furniture, float volume_furniture, float length, float width, float depth)
    {
        this->furniture=new char[strlen(furniture)+1];
        strcpy(this->furniture,furniture);
        this->volume_furniture=volume_furniture;
        this->length=length;
        this->width=width;
        this->depth=depth;
    }
    
    
    Furniture::~Furniture()
    {
        if (this->furniture!=NULL) {
            delete[] this->furniture;
        }
    }
    
    
    char* Furniture:: Getfurniture()
    {
        return furniture;
    }
    void Furniture:: Setfurniture(char* val)
    {
        furniture=val;
    }
    float Furniture:: Getvolume_furniture()
    {
        return volume_furniture;
    }
    void Furniture:: Setvolume_furniture()
    {
        volume_furniture=length*width*depth;
    }
    float Furniture:: Getlength()
    {
        return length;
    }
    void Furniture:: Setlength(float val)
    {
        if (val<0) {
            length=0;
        } else {
        length=val;
        }
    }
    float Furniture:: Getwidth()
    {
        return width;
    }
    void Furniture:: Setwidth(float val)
    {
        if (val<0) {
            width=0;
        } else {
        width=val;
        }
    }
    float Furniture:: Getdepth()
    {
        return depth;
    }
    void Furniture:: Setdepth(float val)
    {
        if (val<0) {
            depth=0;
        } else {
        depth=val;
        }
    }
    
    
    void Furniture::print()
    {
        printf("Characteristics of the furniture: \n");
        printf("Furniture: %s \n", this->furniture);
        printf("Height: %.2f \n", this->Getlength());
        printf("Width: %.2f \n",this->Getwidth());
        printf("Depth: %.2f \n", this->Getdepth());
        printf("Volume of the furniture: %.2f \n", this->Getvolume_furniture());
    }

    The program works, I know, I still have the pointers but with pointers the program works, I will fix it tomorrow (it's night in Spain). But... there is still something I have to fix: the program prints what I declared in int main(), a word Furniture, the declared values of length, width, depth and the volume. What I need to do is... a description that should be inputted through keyboard and the values should be inputted through keyboard as well. I know how to do that in int main() but now with classes I am lost. Could you help me to fix it? and we will finally solve the problem.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,446
    Could you help me to fix it? and we will finally solve the problem.
    I can as soon as you get rid of all those pointers, C-stdio calls and most everything else I suggested.

    Did you even look at the code I supplied in my last post?



    Jim

  6. #21
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    I can as soon as you get rid of all those pointers, C-stdio calls and most everything else I suggested.

    Did you even look at the code I supplied in my last post?

    Jim
    I was writing at the time you posted that code. Seems that I spent so much time typing and when I submitted the reply the forum asked me to sign in again and I lost all I wrote. Hopefully I restored the message. I took a look at your code, I don't know why you put 2.0 to all the properties but yes, I know our codes and my instructor seems to be very obsolete from 90s , I am aware of that, nobody today use printf...

  7. #22
    Registered User
    Join Date
    Nov 2015
    Posts
    112
    Good code. All clear, all clear immediately.

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    4,446
    I don't know why you put 2.0 to all the properties
    That is initializing those variables to 2.0, I just chose this value (2.0) so that the compute_volume() function would show some reasonable value.

    I know our codes and my instructor seems to be very obsolete from 90s , I am aware of that, nobody today use printf...
    Then you really really need to see about finding a better instructor. What you're being taught will not help you when you want to find a job programming in C++.

    Jim

  9. #24
    Registered User
    Join Date
    May 2017
    Posts
    101
    I am a Chemical Engineer but unemployed so I decided to take that course to gain a new skill. I see sometimes Chemical Engineer jobs require programming skills I don't know if at the level of computing professionals but I am not looking for jobs in IT because I lack of education. I recently registered to enroll in a Master's Degree so having so much free time I decided to take this course. However, I don't know what to do with my life, is not easy to find a job in my field, mostly because requires years of experience I don't own. I even tried to look for jobs overseas, including Singapore (if laser is reading this, maybe can tell me how much chemical engineers are in demand there).

    Anyway, it's too late. I will continue with this exercise tomorrow, I promise you to do my best. Thank you very much Jim.

  10. #25
    Registered User
    Join Date
    May 2017
    Posts
    101
    This is the code you asked me:

    First File: main.cpp

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        Furniture j1;
        j1.set_name("Furniture");
        std::cout << "The name of the item is: " << j1.get_name() << std::endl;
    
        j1.Setlength(10);
        j1.Setwidth(20);
        j1.Setdepth(30);
        j1.compute_volume();
    
        j1.print();
    
        return 0;
    }
    Second File: Furniture.h

    Code:
    #ifndef FURNITURE_H
    #define FURNITURE_H
    #include <string>
    
    class Furniture
    {
        public:
            Furniture() = default;
    
            std::string get_name() const;
            void set_name(const std::string& val);
            double compute_volume();
            double Getlength();
            void Setlength(double val);
            double Getwidth();
            void Setwidth(double val);
            double Getdepth();
            void Setdepth(double val);
            void print();
    
        private:
            std::string name;
            double length = 2.0;
            double width = 2.0;
            double depth = 2.0;
    };
    
    
    #endif // FURNITURE_H
    Third File: Furniture.cpp

    Code:
    #include <string>
    #include <iomanip>
    
    std::string Furniture::get_name() const
    {
        return name;
    }
    
    void Furniture::set_name(const std::string& item_name)
    {
        name = item_name;
    }
    
    double Furniture:: Getlength()
    {
        return length;
    }
    
    void Furniture:: Setlength(double val)
    {
        if (val<0) {
            length=0;
        } else {
        length=val;
        }
    }
    
    double Furniture:: Getwidth()
    {
        return width;
    }
    void Furniture:: Setwidth(double val)
    {
        if (val<0) {
            width=0;
        } else {
        width=val;
        }
    }
    double Furniture:: Getdepth()
    {
        return depth;
    }
    void Furniture:: Setdepth(double val)
    {
        if (val<0) {
            depth=0;
        } else {
        depth=val;
        }
    }
    
    double Furniture::compute_volume()
    {
        return depth * width * length;
    }
    
    void Furniture::print()
    {
        cout << setprecision(2) << fixed;
        cout << "\nPrint using a function\n" << "Furniture:  " << name;
        cout << "\nVolume of furniture:  " << compute_volume();
        cout << "\nLength: "  << length;
        cout << "\nWidth: " << width;
        cout << "\nDepth: " << depth;
    }
    However I get two errors in int main():

    main.cpp|7|error: 'Furniture' was not declared in this scope|
    main.cpp|8|error: 'j1' was not declared in this scope|

    As we talked yesterday, instead of declaring a name and values for length, width and depth I need to solve it inputting a description of the furniture and values for length, width and depth through keyboard. Thank you in advance.
    Last edited by JorgeChemE; 05-27-2017 at 02:46 AM.

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,446
    main.cpp|7|error: 'Furniture' was not declared in this scope|
    main.cpp|8|error: 'j1' was not declared in this scope|
    What #include files are you using in main.cpp?


    As we talked yesterday, instead of declaring a name and values for length, width and depth I need to solve it inputting a description of the furniture and values for length, width and depth through keyboard.
    Okay, you have all of those setter functions why not just get the information from the user in main() and use some of those functions to set the values in the class?

    Also all of your get() functions should be const qualified the same as get_name(). And instead of using the 2.0 to initialize the width, length, depth you probably should just use zero.

    Lastly look a this snippet from main():
    Code:
        j1.Setdepth(30);
        j1.compute_volume();
    Do you realize that compute_volume is really not doing anything in this content? You have a function that is returning a value that you don't use and doesn't modify any of the class variables so there is no need to call it here. It only needs to be called when you need to use the computed value. If you notice this function is being called in the "display" function to compute the volume and display the result.

    Do you understand all of the code you have copied and pasted into your program?

    Jim

  12. #27
    Registered User
    Join Date
    May 2017
    Posts
    101
    I have to postpone this. I am working in a project using a structure (see the topic How to add and delete items in a structure? in C section). I promise to come back later to work on this. Thank you for your comprehension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [help] Object-Oriented Programming
    By null321 in forum C++ Programming
    Replies: 4
    Last Post: 08-02-2009, 12:29 PM
  2. What is Object Oriented Programming?
    By sunrising in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2008, 07:58 AM
  3. Object Oriented Programming
    By obaid in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2007, 05:31 AM
  4. Object Oriented Programming
    By eric123 in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 04:56 AM
  5. C++ and Object Oriented Programming
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2002, 05:27 AM

Tags for this Thread