Thread: Need help (migrating from java)

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Need help (migrating from java)

    I'm used to working with java hiearchies. I understand there is no super() in C++, but can I still design the same type of hiearchy, i.e. Creating an abstract class, with subclasses calling the superclasses constructor and superclass methods? Or should I not have constructor code in the abstract class, and put constructors in each of the subclasses? My example below rights the code inline within the class. Should I be making use of "friends" in C++ as well? Or can I pretty much do everything I need like in my example C++ code below? thanks!

    Code:
    Java:
    
    abstract class Test
    {
       private int a, b;
    
       public Test( int a, int b )
       {
             this.a = a;
             this.b = b;
        }
    }
    
    class TestChild extends Test
    {
      private int c;
    
      public TestChild( int a, int b, int c )
      {
             super( a, b );
             this.c = c;
       }
    }
    
    C++ :
    
    class Test
    {
        private:
             int a, b;
    
        public:
             Test( int a, int b ){ this->a = a, this->b = b; }
             virtual void print=0; //make abstract
    }
    
    class TestChild :: public Test
    {
        private:
             int a, b, c
     
        public:
              TestChild( int a, int b, int c )
             { 
                         Test::Test( a, b );  //this the right way to do it?  call superclass constructor?
                         this->c = c;
               }
              
              void print(){ cout << "I am printing..."; }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would do:
    Code:
    class Test
    {
        private:
             int a, b;
    
        public:
             Test( int a, int b ){ this->a = a, this->b = b; }
             virtual void print=0; //make abstract
    }
    
    class TestChild :: public Test
    {
        private:
             int a, b, c
     
        public:
              TestChild( int a, int b, int c ) : Test(a, b)
             { 
                         this->c = c;
               }
              
              void print(){ cout << "I am printing..."; }
    }
    Also, I prefer to use different names for the arguments to the constructor and the class members.

    E.g:
    Code:
              TestChild( int aa, int ab, int ac ) : Test(aa, ab)
             { 
                         c = ac;
               }
    That way, you don't need to use "this->" all over the place.

    Neither of my suggestions will affect what happens.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130

    Lightbulb

    Quote Originally Posted by John_L View Post

    Code:
    class TestChild : public Test
    {
        private:
             int a, b, c
     
        public:
              TestChild( int a, int b, int c ) : Test( a, b ) 
              {
                         this->c = c;
              }
              
              void print(){ cout << "I am printing..."; }
    }
    In C++ there is no "super" keyword, you need to know the base class' name ( probably because you can have more than one base class, so "super" would be ambigous ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    ok thanks. Couple more questions:

    1) Matsp: Does it make a difference if you include it in the method header versus the method
    body (as i did)?

    2) When does using "friends" become necessary?

    3) What about when you start having multiple subclasses, which extend a subclass of the base class? Do you travel up the hierarchy the same way, or can the Test 3 actually envoke base Class Test methods itself?

    ex)

    Code:
    class Test
    
    class Test2 : public Test
    
    class Test 3 : public Test2

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by John_L View Post
    ok thanks. Couple more questions:

    1) Matsp: Does it make a difference if you include it in the method header versus the method
    body (as i did)?
    It makes no functional difference - it's just "style".

    2) When does using "friends" become necessary?
    When a class or function needs to access private data of another class that it's not a member of.
    E.g.:
    Code:
    class X
    {
    private:
       int x;
    ...
       friend void print(X a);
    ...
    }
    
    void print(X a)
    {
       cout << a.x << endl;
    }

    3) What about when you start having multiple subclasses, which extend a subclass of the base class? Do you travel up the hierarchy the same way, or can the Test 3 actually envoke base Class Test methods itself?

    ex)

    Code:
    class Test
    
    class Test2 : public Test
    
    class Test 3 : public Test2
    [/QUOTE]

    Normally, Test3 calls Test2's constructor, Test2 then calls Test constructor. Either implicitly or explicitly. But there should be no need for Test3 to call Test explictly - you shouldn't need to know about Test inside Test3.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    Thanks a lot, you really helped clear things up for me. My C++ program is coming along nicely!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. First Java Class at college
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 09-29-2004, 10:38 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM