Point of OOP?

This is a discussion on Point of OOP? within the C++ Programming forums, part of the General Programming Boards category; What exactly is the point of classes? If I created a class that added two numbers (I might make some ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Point of OOP?

    What exactly is the point of classes? If I created a class that added two numbers (I might make some syntactical errors here)

    Code:
    class addition {
    public:
    double add(double a,double b) {
    c = a + b;
    return c;
    }
    }Add1; 
    answer = Add1.add(3,5);
    I could achieve the same effect by making a function outside of a class! So why use classes? Sorry about being cynical. I just need this cleared up for me :P

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, you WOULDN'T make a class that just did something trivial like that.

    There is a fourfold set of goals of OOP:

    1. Encapsulation. All data and methods related to that data are stored in a class. Classes interact through their public interfaces. The internals of how the data is stored is irrelevent to the user of the class; as long as the methods do what they should, the internal data structure is unimportant. As long as the interface is unchanged, I can modify the data storage however I want, and I only need change the code for that class; no other classes will need a code modification at all.

    2. Inheritance. Classes can form a hierarchy -- for example, class Monster might be a subclass of class Actor, as might class Player. In this way, Player and Monster both inherit all the data and the interface of Actor, but can expand the class as necessary.

    3. Abstraction. Because a Player is-an Actor, you can use a Player object or a Monster object when your function needs an Actor.

    4. Polymorphism. Every class derived from Actor may need to have the same method, but the method should do different things depending on the class. For example, a GetNextMove() command might use a scripted AI in the Monster class, but prompt the user for an action in the Player class. Yet if you have just a pointer/reference to an Actor, you can call the GetNextMove() method and the correct code will be executed, regardless of whether it was a Player or a Monster.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    chrishowarth :
    That is not OOP, you missed the boat.
    That is merely using a class as a namespace.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Direct quote from wikipedia:

    Object-oriented programming's roots reach all the way back to the creation of the Simula programming language in the 1960s, when the nascent field of software engineering had begun to discuss the idea of a software crisis. As hardware and software became increasingly complex, how could software quality be maintained? Object-oriented programming addresses this problem by strongly emphasizing modularity in software.[1]

    Object-oriented programming may be seen as a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a list of instructions to the computer. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent little machine with a distinct role or responsibility.[2]

    Object oriented programming is intended to promote greater flexibility and maintainability in programming, and is widely popular in large-scale software engineering.[citation needed] By virtue of its strong emphasis on modularity, object oriented code is intended to be simpler to develop and easier to understand later on, lending itself to more direct analysis, coding, and understanding of complex situations and procedures than less modular programming methods.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Well, you WOULDN'T make a class that just did something trivial like that.
    chrishowarth :
    That is not OOP, you missed the boat.
    That is merely using a class as a namespace.
    Yes but that was for the point of example, not for a practical use. Even creating a function for addition is pointless, let alone using a class. But you have to understand that I wasn't going to spend half an hour thinking of a useful complicated class for use in a quick post

    But thanks guys, I think I can see the point in organising code into OOP structures now. I just needed that cleared up a bit.

    ~ Christopher Howarth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 12:06 PM
  4. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 06:10 AM
  5. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 10:49 AM

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