Thread: How to learn Object Oriented Programming / Classes?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    How to learn Object Oriented Programming / Classes?

    Hi,

    Today my course started with OOP (Object Oriented Programming) or classes, do you know what I mean? (sometimes I find difficult translating some specific terms). I have an easy exercise to calculate the volume of a furniture (length x width x deep) using OOP but I need to understand the neccesary theory to solve it. Where to start?

    Thank you in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you learning C, or another programming language that has built-in object oriented programming support, e.g., C++?
    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

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    I am only learning C/C++.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JorgeChemE
    I am only learning C/C++.
    You should read Bjarne Stroustrup's answer to the FAQ: What do you think of C/C++?

    As such, I shall assume that you are actually learning C++, so I shall move this to the C++ programming forum.

    Quote Originally Posted by JorgeChemE
    I have an easy exercise to calculate the volume of a furniture (length x width x deep) using OOP but I need to understand the neccesary theory to solve it. Where to start?
    OOP sounds like a wrong tool for computing the volume of furniture, unless you are modeling different (sub)types of furniture (table, chair, bean bag, odd-shaped sofa, etc), and wish to present an interface for computing the volume of furniture without having to inspect the exact subtype of furniture in order to make the computation. In such a case, you might have an abstract base class named Furniture that has a pure virtual member function, e.g., compute_volume() that subclasses override with the precise volume computation implementation appropriate for them.

    I suggest reading these articles (PDF documents):
    The Open-Closed Principle
    The Liskov Substitution Principle
    The Dependency Inversion Principle
    The Interface Segregation Principle
    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

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by laserlight View Post
    You should read Bjarne Stroustrup's answer to the FAQ: What do you think of C/C++?

    As such, I shall assume that you are actually learning C++, so I shall move this to the C++ programming forum.


    OOP sounds like a wrong tool for computing the volume of furniture, unless you are modeling different (sub)types of furniture (table, chair, bean bag, odd-shaped sofa, etc), and wish to present an interface for computing the volume of furniture without having to inspect the exact subtype of furniture in order to make the computation. In such a case, you might have an abstract base class named Furniture that has a pure virtual member function, e.g., compute_volume() that subclasses override with the precise volume computation implementation appropriate for them.

    I suggest reading these articles (PDF documents):
    The Open-Closed Principle
    The Liskov Substitution Principle
    The Dependency Inversion Principle
    The Interface Segregation Principle
    Thank you for your support laser, but what I need is much easier than that. I only need to make this program: calculate the volume of a furniture supposing that the volume is: length, width, depth and can be calculated as: length x width x depth. I should also put a description of the furniture through keyboard.

    Well, if this exercise can be done in int main () or using functions or structure I would solve it in a matter of minutes. The problem is that must be solved using object oriented programming / classes and I just started with that and cost me a hell to understand. Moreover, I am using Code Blocks software and creating a new class, do you know this software? I am trying to solve it based on an example we have done. If I have something I will come back later.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JorgeChemE
    I only need to make this program: calculate the volume of a furniture supposing that the volume is: length, width, depth and can be calculated as: length x width x depth.

    Well, if this exercise can be done in int main () or using functions or structure I would solve it in a matter of minutes.
    You should solve it by writing a main function that just does the job.

    If that is unacceptable, then your instructor is teaching you OOP badly, and I propose this Java-inspired "solution":
    Code:
    class Main
    {
    public:
        double compute_volume(double length, double width, double depth)
        {
            // ...
        }
    
        void doWork()
        {
            // call compute_volume somewhere
            // ...
        }
    };
    
    int main()
    {
        Main object;
        object.doWork();
    }
    Tada! "Object oriented programming"!

    I am afraid that I cannot explain to you how this is OOP because... it isn't really OOP. It is just your usual procedural programming under the veneer of a class and an object of the class.

    EDIT:

    Well, okay. I suspect that what your instructor had in mind was the creation of a Furniture object with the three attributes and a compute_volume member function to compute the volume. This is arguably "OOP", but at a level that isn't very different from the typical kind of structure manipulation that is also part of primarily procedural languages like C. Plus, the name "Furniture" is arguably too generic to make assumptions that the volume is that of a cuboid.
    Last edited by laserlight; 05-26-2017 at 05:42 AM.
    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

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    101
    Laser, the best way I have to explain you what I need is with an example. This is what I have done, my first approach without any success. Hope that is easier to understand if I put you a code, but since I don't know how to post it since I have three files I will put them separately:

    First file: main.cpp

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Furniture.h"
    
    
    using namespace std;
    
    
    void print(Furniture *j)
    {
        printf("\nPrint using a function\n");
        printf("Furniture: %s ", j->Getfurniture());
        printf("Volume of furniture: %.2f ", j->Getvolume_furniture());
        printf("Length: %.2f ", j->Getlength());
        printf("Width: %.2f ", j->Getwidth());
        printf("Depth: %.2f", j->Getdepth());
    }
    
    
    int main()
    {
        Furniture *j1 = new Furniture();
        j1->Setfurniture("Furniture");
        j1->Setvolume_furniture(volume_furniture);
        j1->print();
    
    
        j1->Setlength(10);
        j1->print();
    
    
        j1->Setwidth(20);
        j1->print();
    
    
        j1->Setdepth(30);
        j1->print();
    
    
        return 0;
    }
    Second file: Furniture.h

    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();//Destructor
    
    
            char* Getfurniture();
            void Setfurniture(char* val);
            float Getvolume_furniture();
            void Setvolume_furniture(float val);
            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

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Furniture.h"
    
    
    Furniture::Furniture()
    {
        this->furniture=NULL;
        this->volume_furniture=0.0;
        this->length=0.0;
        this->width=0.0;
        this->depth=0.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(float val)
    {
        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 Setdepth(float val)
    {
        if (val<0) {
            depth=0;
        } else {
        depth=val;
        }
    }
    This is only an attempt to adapt a code that stores names, ages and heigths of people to this exercise that asks a description and a volume of an object. By the way, I failed even declaring the description and the values of measurements of the object and I should ask the user to input them through keyboard but I am still trying this way before proceeding to ask data through keyboard.

    I created a new class in Code Blocks, I have the classes public, protected and private (we still don't use protected until the next week, we are still learning classes/OOP), "Getters" and "Setters".

    I hope with this information you have a little idea of what I am doing.
    Last edited by JorgeChemE; 05-26-2017 at 06:11 AM.

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    101
    Sorry for omitting where is the error: is in int main(), line 25

    Code:
    j1->Setvolume_furniture(volume_furniture);
    Last edited by JorgeChemE; 05-26-2017 at 07:51 AM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you're learning OOP in C++ then isn't it time to leave C behind and start using C++?

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void print(const Furniture& j)
    {
        cout << "\nPrint using a function\n" << "Furniture:  " << j.Getfurniture();
        cout << "Volume of furniture:  " << j.Getvolume_furniture();
        cout << "Length: " << setprecision(2) << fixed << j.Getlength();
        cout << "Width: " << j.Getwidth();
        cout << "Depth: " << j.Getdepth();
    }
    Why are you using a the pointers? In C++ pointers should be avoided whenever possible. Prefer to pass by reference over passing by pointers.

    Look at this snippet:
    Code:
    int main()
    {
        Furniture *j1 = new Furniture();
        j1->Setfurniture("Furniture");
        j1->Setvolume_furniture(volume_furniture);  
        j1->print();
    Again why the pointer? Why not just use a "normal" non-pointer instance?

    Code:
    ...
    int main()
    {
        Furniture j1;
        j1.Setfurniture("Furniture");
        j1.Setvolume_furniture(volume_furniture);
        j1.print();
    ...
    Also where have you defined a variable named volume_furniture?

    And where have you defined and implemented a class member function named print(), all I see is the global function print().

    And you really should be pushing your instructor to start using modern C++ classes like std::string instead of using the more dangerous C style strings.


    Jim

  10. #10
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    Also where have you defined a variable named volume_furniture?

    And where have you defined and implemented a class member function named print(), all I see is the global function print().

    And you really should be pushing your instructor to start using modern C++ classes like std::string instead of using the more dangerous C style strings.


    Jim
    Fixed the pointers. That's one of my questions, I don't know where to define volume_furniture (to calculate the volume), this is my first attempt using classes. All I know is that I calculate the volume in Furniture.cpp.

    The function print() is in public and a global function as you mentioned is in main. Should I implement "a class member function named print()"? where?

    Edited: if helps this is the error I get:

    |23|error: no matching function for call to 'Mueble::Setvolume_furniture()'|
    |23|note: candidate is:|
    include\Furniture.h|15|note: void Mueble::Setvolumen_mueble(float)|
    Last edited by JorgeChemE; 05-26-2017 at 10:59 AM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Edited: if helps this is the error I get:

    |23|error: no matching function for call to 'Mueble::Setvolume_furniture()'|
    So where have you defined a class named Mueble that has a Setvolume_furniture() member function?

    |23|note: candidate is:|
    include\Furniture.h|15|note: void Mueble::Setvolumen_mueble(float)|
    You may want to check your spelling?

    That's one of my questions, I don't know where to define volume_furniture (to calculate the volume), this is my first attempt using classes.
    Then perhaps this should be a class member function?

    Should I implement "a class member function named print()"?
    IMO, the function is fine as is (without the pointer and without the printf()). Since you have accessor functions there is no real reason this function should be part of the actual class.


    Jim

  12. #12
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    So where have you defined a class named Mueble that has a Setvolume_furniture() member function?


    You may want to check your spelling?


    Then perhaps this should be a class member function?


    IMO, the function is fine as is (without the pointer and without the printf()). Since you have accessor functions there is no real reason this function should be part of the actual class.


    Jim
    OMG Jim... sorry sorry and sorry. My code is in spanish so I copied and pasted the error in spanish, "mueble" is furniture here.

    Well I detected two things could be relevant. The first one is that in Furniture.cpp I forgot the library #include <string.h> (this was a huge problem). The second one, I removed from:
    void Setvolume_furniture(float val); float val in all places that is present and now is: void Setvolume_furniture(). I was hopping that the program finally compile something but... says that in Furniture.cpp depth is not declared in this scope. Why? I don't understand anything, why depth and not length and width?

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    sorry sorry and sorry. My code is in spanish so I copied and pasted the error in spanish, "mueble" is furniture here.
    It would probably be better if you just posted the actual code and error messages without trying to translate things.

    void Setvolume_furniture(float val); float val in all places that is present and now is: void Setvolume_furniture(). I was hopping that the program finally compile something but... says that in Furniture.cpp depth is not declared in this scope. Why? I don't understand anything, why depth and not length and width?
    Post your current code.

    Jim

  14. #14
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    It would probably be better if you just posted the actual code and error messages without trying to translate things.



    Post your current code.

    Jim
    Ok, just give me some minutes, the last error was because in voidSetdepth(floatval) I was missing Furniture::, fixed but something is missing with print() function. Give me some minutes to post my current and latest code. I feel we are close to solve it.

  15. #15
    Registered User
    Join Date
    May 2017
    Posts
    101
    First file: main.cpp


    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Furniture.h"
    
    
    using namespace std;
    
    
    void print(Furniture *j)
    {
        printf("\nPrint using a function\n");
        printf("Furniture: %s ", j->Getfurniture());
        printf("Volume of furniture: %.2f ", j->Getvolume_furniture());
        printf("Length: %.2f ", j->Getlength());
        printf("Width: %.2f ", j->Getwidth());
        printf("Depth: %.2f", j->Getdepth());
    }
    
    
    int main()
    {
        Furniture *j1 = new Furniture();
        j1->Setfurniture("Furniture");
        j1->Setvolume_furniture(volume_furniture);
        j1->print();
    
    
        j1->Setlength(10);
        j1->print();
    
    
        j1->Setwidth(20);
        j1->print();
    
    
        j1->Setdepth(30);
        j1->print();
    
        delete j1;
    
        return 0;
    }

    Second file: Furniture.h


    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


    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;
        }
    }
    This is the code of the 3 files (main.cpp, Furniture.h, Furniture.cpp). Note that I am still using pointers but I will fix it later, I think the pointers are ok but I will change them with your recommendations if I achieve to solve this. Even if the program runs, there is still one thing I need to fix: data should be inputted through keyboard instead of declaring values for length, width, depth. Another thing I fixed is that in constructor I declared all the starting values with 1 because I read somewhere else is better than 0 (except the volume to calculate).

    I appreciate any feedback. Thanks!
    Last edited by JorgeChemE; 05-26-2017 at 12:05 PM.

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