?
Solved

MagicNumbers Code Correction

Posted on 2004-04-12
9
Medium Priority
?
200 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 2000 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
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!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.
Suggested Courses

770 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