SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: C++ Inheritance

  1. #1
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C++ Inheritance

    Hello everyone, wasn't too sure if anyone knew any c++ here or not, but I'm having a problem with inheritance. Here is my problem:

    I have my class, with private variables int and double, but if I want to initialize both with one of my constructors that has a parameter set to, int and int, how could I do that? I'll paste an example below:

    Code Cpp:
    class something {
    public:
     void something(int, int);
    private:
     int x;
     double y;
    };
     
    // and here how would I create a function of "something" to initialize x and y to the parameters int, int?

    thanks for any help.

  2. #2
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean just filling in the constructor?

    You'd do something like (in the implementation file):
    Code Cpp:
    something::something( int x, double y )
    {
      this->x = x;
      this->y = y;
    }

    Or were you asking another question ( the title seems to be misleading/contradicting? )

  3. #3
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there, thanks for writing me back,

    the problem was that the private member variables are int and double, and I need to write a function that initialize both of those variables to the function with parameters int and int;

    Code Cpp:
    something::something(int a, int b) {
     
     // how can I initialize the double variable with an int parameter?
     
    }

    thanks for the help. (PS) Could I use the static cast?

  4. #4
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh I see, yes you'd use a static cast, as in:
    Code Cpp:
    this->y = static_cast<double>(y);

  5. #5
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess the title is not "correct" but would I use "this ->" ? or could I use:

    Code Cpp:
    something::something(int a, int b) {
     
     x = a; // int variable x
     static_cast<double>(y) = b; // double variable y
     
    }

    Thanks for your help I really appreciate it.

  6. #6
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I much prefer m_ prefix for members to cut out the ambiguity (which is why I used this->)

  7. #7
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    c++ is a nightmare for web apps, too much care has to be taken to manage alloc/dealloc of memory...a memory leak on a server app can crash a server in jig-time...there are plenty more web-friendly alternatives such as perl/php/python all which support OO development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •