Solved

MagicNumbers Code Correction

Posted on 2004-04-12
9
190 Views
Last Modified: 2010-04-01
Need help in correcting actual code.  When I try to compile unix displays a consortium of errors.  Please advise.  Del

#include <iostream>

using namespace std;

class MagicNumbers {

public:

   MagicNumbers(int x, int y, int z) { // inline function
      a = x; b = y; z = b;

   }

   void show_numbers(void) {
      cout << a << ' ' << b << ' ' << c << '\n';

   }

private:

   int a, b, c;

};

int main()
{
   int one, two, happy;
   MagicNumbers one(1, 1, 1);
   MagicNumbers two(202, 202);
   MagicNumbers default;
   MagicNumbers happy(101, 101, 101);
   one.show_numbers();             // 1, 1, 1
   two.show_numbers();              // 202, 202, 3
   defaults.show_numbers();                      // 1, 2, 3
   happy show_numbers();             // 101, 101, 101

   return 0;

}


0
Comment
Question by:edelossantos
[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
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10809796
This should be more like

#include <iostream>

using namespace std;

class MagicNumbers {

public:

  MagicNumbers(int x, int y, int z) { // inline function
    a = x; b = y; c = b;

  }
  MagicNumbers() { // inline function. default constructor
    a = 0; b = 0; c = 0;

  }

  void show_numbers(void) {
    cout << a << ' ' << b << ' ' << c << '\n';

  }

private:

  int a, b, c;

};

int main()
{
  int nOne, nTwo, nHappy;
  MagicNumbers one(1, 1, 1);
  MagicNumbers two(202, 202, 202);
  MagicNumbers defaults;
  MagicNumbers happy(101, 101, 101);
  one.show_numbers();           // 1, 1, 1
  two.show_numbers();            // 202, 202, 3
  defaults.show_numbers();                     // 1, 2, 3
  happy.show_numbers();           // 101, 101, 101

  return 0;

}
0
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10809803
1. int one, two, happy;
   MagicNumbers one(1, 1, 1);
   MagicNumbers two(202, 202);

you are first declaring one, two as type int ..... later declaring them as type MagicNumbers

2. MagicNumbers two(202, 202);
you need three constructors for your class taking 0, 2, 3 parameters

#include <iostream>

using namespace std;

class MagicNumbers {

public:

   MagicNumbers(int x, int y, int z) { // inline function
     a = x; b = y; z = b;
   }

   MagicNumbers(int x, int y) { // inline function
     a = x; b = y; c=3;
   }

   MagicNumbers() { // inline function
     a = 1; b= 2; c = 3;
   }


   void show_numbers(void) {
     cout << a << ' ' << b << ' ' << c << '\n';

   }

private:

   int a, b, c;

};

int main()
{
      MagicNumbers one(1, 1, 1);
   MagicNumbers two(202, 202);
   MagicNumbers default;
   MagicNumbers happy(101, 101, 101);
   one.show_numbers();           // 1, 1, 1
   two.show_numbers();            // 202, 202, 3
   default.show_numbers();                     // 1, 2, 3
   happy.show_numbers();           // 101, 101, 101

   return 0;

}
0
 
LVL 1

Expert Comment

by:hal3000
ID: 10809845
Hi edelossantos,

In addition, in the class you need the declarations of a, b, and c to precede the method declarations.

Good luck
0
Technology Partners: 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!

 

Author Comment

by:edelossantos
ID: 10809863
I wish I could have picked the two of you.  Both solutions were very helpful and insightfull.  Thank you.  Del
0
 
LVL 1

Expert Comment

by:hal3000
ID: 10809913
You can using point split
0
 

Author Comment

by:edelossantos
ID: 10809930
I am still new at this, how do you point split?
0
 
LVL 1

Expert Comment

by:hal3000
ID: 10809938
See the help section on closing questions.

But At this point you have to make a request to the moderator.
0
 

Author Comment

by:edelossantos
ID: 10809949
Thank you.  Del
0
 

Author Comment

by:edelossantos
ID: 10809969
jkr
    Your solution and resolution to the problem was excellent, and was exactly what I needed.  Thank you once again.  Del
0

Featured Post

Independent Software Vendors: 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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

733 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