Solved

MagicNumbers Code Correction

Posted on 2004-04-12
9
185 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
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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