Solved

MagicNumbers Code Correction

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now