Thread: abstract class

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    abstract class

    Hello everybody.
    I have been asked by my employer to discuss the issue of abstract classes.
    I found abstract classes in Java and abstract base classes in C++.
    can you provide some insight on the following questions.

    (1)
    Is this a valid definition...
    Abstract class is a general term of a class implimentation focused on efficiency of a certain task. example of these are the above java abstract class, and c++ abstract base classes.

    (2)
    at what level of education or name of class does you primary study turn to abstract classes? i have finished almost every programming language class at my school, but have never seen this concept.

    (3)
    what is real life example of the efficiency gain from using an abstract class?

    (4)
    As for general class concepts like inheritence and polymorphism. Would these examples still be similar in this topic since they are all class?

    Thank you for your assistance.
    Last edited by xviddivxoggmp3; 12-30-2004 at 10:46 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I´ll try to explain my opinion in my poor english: abstract class is a class that groups many classes with their particularity, but have something in common. But the abstract class itself, does not have any useful meaning, you must use derived classes to achieve semantic. For example, workers in a factory. You could have the maintance staff, the conrol staff, so on... all they share some information, for example payment, sector in the factory. Each has some specific info, for example, the maintance staff need specific tools, the control staff control specific sectors.

    Hope that help!

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >Is this a valid definition...
    No, not really. An abstract class in C++ and Java is simply an interface. It provides a formal set of operations that derived classes can implement as they see fit, as long as the interface is maintained. Abstract classes are abstract because objects of those classes don't make sense. They only serve to provide an interface for derived classes.

    >at what level of education or name of class does you primary study turn to abstract classes?
    Abstract classes in C++ should be introduced shortly after virtual functions in my opinion. This provides a base to build on while introducing a useful feature relatively early. However, the order of instruction is completely up to the school and/or instructor.

    >what is real life example of the efficiency gain from using an abstract class?
    OOP is strictly an organizational tool, and as such will typically only effect programmer productivity unless you get into the nitty-gritty of language features. What exactly do you mean by "efficiency gain"?

    >Would these examples still be similar in this topic since they are all class?
    Well, I like to make a distinction between classes as an encapsulation tool and classes as a project organizational tool. But yes, abstract classes are closely related to inheritance and polymorphism.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    >what is real life example of the efficiency gain from using an abstract class?

    A real life example of abstract base classes are COM objects. These are implemented in C++ via abstract base classes.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    A little word from Stroustrup:

    Many classes resemble class Employee in that they are useful both as themselves and also as bases
    for derived classes. For such classes, the techniques described in the previous section suffice.
    However, not all classes follow that pattern. Some classes, such as class Shape, represent abstract
    concepts for which objects cannot exist. A Shape makes sense only as the base of some class
    derived from it. This can be seen from the fact that it is not possible to provide sensible definitions
    for its virtual functions:
    Code:
    class Shape {
    public:
    virtual void rotate(int) { error("Shape: :rotate") ; } / / inelegant
    virtual void draw() { error("Shape: :draw") ; }
    / / ...
    };
    Trying to make a shape of this unspecified kind is silly but legal:
    Code:
    Shape s; / / silly: ‘‘shapeless shape’’
    It is silly because every operation on s will result in an error.
    A better alternative is to declare the virtual functions of class Shape to be pure virtual functions.
    A virtual function is ‘‘made pure’’ by the initializer = 0:
    Code:
    class Shape{ / / abstract class
    public:
    virtual void rotate(int) = 0; / / pure virtual function
    virtual void draw() = 0; / / pure virtual function
    virtual bool is_  closed() = 0; / / pure virtual function
    / / ...
    };
    A class with one or more pure virtual functions is an abstract class, and no objects of that abstract
    class can be created:
    Code:
    Shape s; / / error: variable of abstract class Shape
    An abstract class can be used only as an interface and as a base for other classes. For example:
    Code:
    class Point{ /* ... */ };
    class Circle : public Shape {
    public:
    void rotate(int) { } / / override Shape::rotate
    void draw() ; / / override Shape::draw
    bool is_  closed() { return true; } / / override Shape::is_closed
    Circle(Point p, int r) ;
    private:
    Point center;
    int radius;
    }
    A pure virtual function that is not defined in a derived class remains a pure virtual function, so the
    derived class is also an abstract class. This allows us to build implementations in stages:
    Code:
    class Polygon : public Shape{ / / abstract class
    public:
    bool is_  closed() { return true; } / / override Shape::is_closed
    / / ... draw and rotate not overridden ...
    };
    Polygon b; / / error: declaration of object of abstract class Polygon
    class Irregular_  polygon : public Polygon {
    list<Point> lp;
    public:
    void draw() ; / / override Shape::draw
    void rotate(int) ; / / override Shape::rotate
    / / ...
    };
    Irregular_  polygon poly(some_  points) ; / / fine (assume suitable constructor)
    An important use of abstract classes is to provide an interface without exposing any implementation
    details. For example, an operating system might hide the details of its device drivers behind an
    abstract class:
    Code:
    class Character_  device {
    public:
    virtual int open(int opt) = 0;
    virtual int close(int opt) = 0;
    virtual int read(char* p, int n) = 0;
    virtual int write(const char* p, int n) = 0;
    virtual int ioctl(int ...) = 0;
    virtual ~Character_  device() { } / / virtual destructor
    };
    We can then specify drivers as classes derived from Character_ device, and manipulate a variety of
    drivers through that interface.
    With the introduction of abstract classes, we have the basic facilities for writing a complete program
    in a modular fashion using classes as building blocks.

    Hope this helps!

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    5
    > (3) what is real life example of the efficiency gain from using an abstract class?

    Probably ABC (abstract base class) will slow things down a bit. Polymorphism requires all these lovely vtable (virtual function table) look ups and what not. (ABC + Inheritance leads to polymorphism.) But I believe this isn't so much to worry about. Plus, "efficiency gain" would show up on coding side, not as in application execution speed. Clear and intuitive design will save everyone money. ;-)

    For examples of ABC, you could also look at "The C++ Programming Language" by Bjourn Stroustrop (I have a feeling that I mispelled his name. :-) There's a nice example of creating GUI widgets.

    > (4) As for general class concepts like inheritence and polymorphism. Would these examples still be similar in this topic since they are all class?

    Probably you could recycle examples with ABC added. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. abstract class member cannot be overladed?
    By dudeomanodude in forum C++ Programming
    Replies: 10
    Last Post: 12-10-2008, 11:12 AM
  3. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM