Announcement

Collapse
No announcement yet.

Abstract and Concrete Classes

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Abstract and Concrete Classes

    Example of a driver implementation
    Pre_driver is the abstract class
    Driver is the public derived concrete class.

    The functions read and write are own implementations for each derived class that want to implement such operations.
    read and write are in facts pure virtual functions.

    The programmer can create a main source-code without knowing the implementation of write and read functions.

    pre_driver header
    PHP Code:
    //
    //  pre_driver.h
    //  Test
    //
    //  Created by Giovanni Di Grezia on 12/09/13.
    //  Copyright (c) 2013 Giovanni Di Grezia. All rights reserved.
    //

    #ifndef __Test__pre_driver__
    #define __Test__pre_driver__

    #include <iostream>


    class pre_driver {

    public:
        
        
    pre_driver int data ); // constructor
        
    ~pre_driver (); // deconstructor
        
    virtual void write (int dataint value) = 0// abstract function to overload
        
    virtual void read int data)= 0// abstract function to overload
        
    protected:
        
    int base_pointer;
        
    int size;
        

    };


    #endif /* defined(__Test__pre_driver__) */ 
    pre_driver cpp
    PHP Code:
    //
    //  pre_driver.cpp
    //  Test
    //
    //  Created by Giovanni Di Grezia on 12/09/13.
    //  Copyright (c) 2013 Giovanni Di Grezia. All rights reserved.
    //

    #include "pre_driver.h"
    #include <exception>
    #include <string>

    using namespace std;

    ////// constructor definition
    pre_driver::pre_driver int data )
    {
        try{
        if (
    data <=0)
            throw 
    string("Impossibile continuare");
        }
        catch (
    string errore){
            
    cout << errore << endl ;
            exit(-
    1);
        }
        
        
        
    size data;
        
        
    base_pointer = new int [data];


    }


    ////// deconstructor definition
    pre_driver::~pre_driver(){

        
    delete [] base_pointer;


    driver header
    PHP Code:
    //
    //  driver.h
    //  Test
    //
    //  Created by Giovanni Di Grezia on 12/09/13.
    //  Copyright (c) 2013 Giovanni Di Grezia. All rights reserved.
    //

    #ifndef __Test__driver__
    #define __Test__driver__

    #include <iostream>
    #include "pre_driver.h"

    class driver : public pre_driver {

    public:
        
        
    driver(int data);
        
         
    virtual void write (int dataint value);
         
    virtual void read int data);
        

    };


    #endif /* defined(__Test__driver__) */ 
    driver cpp
    PHP Code:
    //
    //  driver.cpp
    //  Test
    //
    //  Created by Giovanni Di Grezia on 12/09/13.
    //  Copyright (c) 2013 Giovanni Di Grezia. All rights reserved.
    //

    #include "driver.h"
    #include <string>

    using namespace std;


    driver::driver (int data)
        :
    pre_driver(data)
        {}


    void driver::read(int data){
        
        
        
        try{
            if (
    data >= size)
                throw 
    string ("Errore");
        }
        catch(
    string errore){
            
    cout << errore << endl;
            exit(-
    1);
        }
        
        
    cout << *(base_pointer data) << endl;



    }


    void driver::write(int dataint value){
        
        try{
        if (
    data >= size)
            throw 
    string ("Errore");
        }
        catch(
    string errore){
            
    cout << errore << endl;
            exit(-
    1);
        }
        
        *(
    base_pointer data) = value;



    main sample
    PHP Code:
    //
    //  main.cpp
    //  Test
    //
    //  Created by Giovanni Di Grezia on 22/07/13.
    //  Copyright (c) 2013 Giovanni Di Grezia. All rights reserved.
    //
    //
    //
    //

    #include <iostream>
    #include <string>
    #include <exception>

    #include "driver.h"

    using namespace std;


        
        
    int main () {
          
            
    driver gino (10); // create object with 10 slot
            
    gino.write(0,1000); // write 1000 to slot 0
            
    gino.read(0); // print content of slot 0
            
            
    gino.write(5,2000); // write 2000 to slot 5
            
    gino.read(5); // read from slot 5
            
            
    return 0;
        } 
    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio
Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia