Solved

MagicNumbers Code Correction

Posted on 2004-04-12
9
196 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

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!

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

719 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