7 Replies - 1391 Views - Last Post: 11 December 2012 - 01:32 PM Rate Topic: -----

#1 sabi20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-December 12

Initializing class member variables?

Posted 08 December 2012 - 09:09 PM

what does these instructions mean when it says "initialize pi with the value 3.14" ..... how can i initialize that variable? The instructions don't list any functions that i can use to set pi to 3.14...... so is this telling me to initialize the pi variable inside the class like i have done below or is there some other way i can do this

class Circle
{
   private:
      double radius;
      double pi = 3.14;
   public:
      Circle();
      Circle(int r);
      void setRadius(int r);
      double getRadius() const;
      double getArea() const;
};


Write a Circle Class that has the following member variables:

radius: a double
pi: a double initialized with the value 3.14

The class should have the following member functions:

Default Constructor - a default constructor that sets radius 0.0
Constructor - accepts the radius of the circle as an argument
setRadius - A mutator method for the radius variable
getRadius - An accessor method for the radius variable
getArea - Returns the area of the circle

Is This A Good Question/Topic? 0
  • +

Replies To: Initializing class member variables?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Initializing class member variables?

Posted 08 December 2012 - 10:52 PM

You can only initialize static const integral types in you class declaration, as is likely required here. Why not just do

const double pi = 3.141;



In you method definition (.cpp) file? There are a multitude of ways to achieve this, but until you understand classes this will suffice.

This post has been edited by ButchDean: 08 December 2012 - 11:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 100
  • Joined: 22-May 12

Re: Initializing class member variables?

Posted 09 December 2012 - 01:45 AM

View Postsabi20, on 08 December 2012 - 09:09 PM, said:

what does these instructions mean when it says "initialize pi with the value 3.14" ..... how can i initialize that variable? The instructions don't list any functions that i can use to set pi to 3.14...... so is this telling me to initialize the pi variable inside the class like i have done below or is there some other way i can do this

class Circle
{
   private:
      double radius;
      double pi = 3.14;
   public:
      Circle();
      Circle(int r);
      void setRadius(int r);
      double getRadius() const;
      double getArea() const;
};


Write a Circle Class that has the following member variables:

radius: a double
pi: a double initialized with the value 3.14

The class should have the following member functions:

Default Constructor - a default constructor that sets radius 0.0
Constructor - accepts the radius of the circle as an argument
setRadius - A mutator method for the radius variable
getRadius - An accessor method for the radius variable
getArea - Returns the area of the circle


I could be wrong but I think this is what its asking you to do...

class Circle
{
   private:
      double radius;
      double pi;
   public:
      Circle();
      Circle(int r);
      void setRadius(int r);
      double getRadius() const;
      double getArea() const;
};

int main()
{
....
....
blah blah
...
}

Circle::Circle(){
      pi = 3.14;
}





Yeah I just read it over again, im pretty sure that what you need to do. I dont think initializing it in your class declaration will even work. Im pretty sure you can only initialize in your default constructor. you will need to include your radius member variable inside the default constructor aswell.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Initializing class member variables?

Posted 09 December 2012 - 08:25 AM

Yes you can, but it's not a constant. Pi is constant by definition.
Was This Post Helpful? 0
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Initializing class member variables?

Posted 09 December 2012 - 09:03 AM

@Midi_ - Note that you didn't initialize your variable here:
Circle::Circle(){
    pi = 3.14;
}

You assigned a value to it, that's different. If you want to initialize it, you need to use initialization list:
Circle::Circle() : pi(3.14) {
  // ...
}

Even if you don't see it at first, there is a difference. See below:

// obj is an object of class B
// value is an object of class C
A::A() {
  obj = value; 
}

Here, first, default constructor B::B() is called, after that obj is fully initialized, then B::operator=(const C& value) is called.

A::A() : obj(value) { }

And here parametrized constructor B::B(const C& value) is called and initializes obj.

See the difference? The first code can even prevent compilation if class B has no fitting operator=.
Was This Post Helpful? -1
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Initializing class member variables?

Posted 09 December 2012 - 11:42 AM

That's introducing undue complexity. It is initialization either way because the member variable is being given a value prior to it being used, as is the definition of initialization.
Was This Post Helpful? 1
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Initializing class member variables?

Posted 09 December 2012 - 11:57 AM

From the Annotated C++ Reference Manual:

Quote

12.6 Initialization

An object of a class with no constructors, no private or protected non-static data members; no virtual functions, and no base classes can be initialized using an initializer list. An object of a class with a constructor must either be initialized or have a default constructor. The default constructor is used for objects that are not explicitly initialized.

12.6.1 Explicit Initialization

Objects of classes with constructors can be initialized with a parenthesized expression list. This list is taken as the argument list for a call of a constructor doing the initialization. Alternatively a single value is specified as the initializer using the = operator. The value is used as the argument to a copy-constructor.

Like Butch said, it doesn't matter how you give a variable a value, the act of giving it a value is initialization.
Was This Post Helpful? 0
  • +
  • -

#8 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Initializing class member variables?

Posted 11 December 2012 - 01:32 PM

Okay, my bad then.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1