Solved

constructors

Posted on 1998-10-25
7
182 Views
Last Modified: 2010-04-02
I have a class:
class number
{
   public:
   number(int one=0,int two=1){num1=one;num2=two;}

   private:
   int num1;
   int num2;
};
  in the constructor above I am trying to get two to default
  to 1 and if it is sent a 0 cahgee it to a 1.
Thanks for the halp in advance.
   
0
Comment
Question by:strmtrpr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 

Expert Comment

by:jbalagop
ID: 1176059
I have a class:
class number
{
   public:
   number(int one=0,int two=1){
           num1=one;
           if(two = 0)
              num2 = 1;
           else
              num2=two;
   }

   private:
   int num1;
   int num2;
};

Is that what you want to do?
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1176060
I'm not sure I understand.  Do you just mean?

class number
   {
      public:
      number(int one=0,int two=1)
     {
         num1=one;
         if (two == 0)
            two = 1;
         num2=two;
     }

      private:
      int num1;
      int num2;
   };


let me know if you have questions.
0
 

Expert Comment

by:jbalagop
ID: 1176061
in the code above : if(two = 0) is actually if(two == 0), but you already knew that.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1176062
Looks like jbalagop beat me to it.  I guess that is what you wanted, his thought was the same as mine.  He was slightly faster, so you might want to reject my answer so he can answer.
0
 

Author Comment

by:strmtrpr
ID: 1176063
I forgot the ==  with one equal sign I was just assinging 0 to two, right?
Thanks for all your help

0
 
LVL 22

Expert Comment

by:nietod
ID: 1176064
Yes, a single = assigns and a double == compares.  
0
 

Author Comment

by:strmtrpr
ID: 1176065
I am slowly starting to get the hang of this I think.
I wanted to thank everyone who answers my questions so quickly.

Thanks again.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question