OOP constructor question

This is a discussion on OOP constructor question within the C++ Programming forums, part of the General Programming Boards category; How can a class constructor initilize a member variable or a class array? dose it have to do with the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    OOP constructor question

    How can a class constructor initilize a member variable or a class array? dose it have to do with the 'this' operator?

    struct default{
    default();
    float num;
    }def[4];

    default::default() {
    ?????
    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could give the struct constructor a default argument -

    Code:
    struct defaulty{ 
    defaulty(float n =10); 
    float num; 
    }def[4]; 
    
    defaulty::defaulty(float n){
    	num=n;
    }
    You'll also want to change the name of your struct, as default is a revserved keyword.
    zen

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Yes but how do i initilize the members of each object. Do i have to make implicit calls.

    default::default() {
    def[0].num = 0;
    def[1].num = 0;
    def[2].num = 0;
    def[3].num = 0;
    }

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    in zens example its already done.
    struct defaulty{
    defaulty(float n =10);
    float num;
    }def[4];

    the def[4] defines an array of 4 defaulty structs. for each struct the constructor will have been called with the default value so therefore it is initialised.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

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, 03:02 AM
  2. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 11:56 AM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  4. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 05:28 PM
  5. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM

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