Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++ class & emun

Posted on 1997-07-24
6
Medium Priority
?
417 Views
Last Modified: 2006-11-17
I am a beginner using boreland C++ and am trying to learn out of a book. Using enum and class, i need to create a class decleration for a traffic light. Assume the light can be in one of three states: green, amber, or red. We should be able to check this state or change it. Please help me by creating this class.   thank you ljmiller@cppj.net
0
Comment
Question by:ljmiller
[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
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:jaba
ID: 1165899
// start header file
class CLight {
protected:
int m_nLightState;
public:
enum STATE {
S_GREEN = 0,
S_AMBER .
S_RED
};
CLight(){};
CLight (intt state ) {m_nLightState = state;};
inline void SetState(int state) {m_nLightState = state;};
inline int GetState const {return m_nLightState};
};

// Start using class
{
CLight lt(lt::S_RED);
lt.SetState(lt:S_AMBER);
....
Thats all

0
 

Author Comment

by:ljmiller
ID: 1165900
the class must not have been ran on boreland c++, because it was full of errors likeCompiling NONAME00.CPP:
Error NONAME00.CPP 10: Storage class 'inline' is not allowed here
Error NONAME00.CPP 10: Declaration missing ;
Error NONAME00.CPP 14: Declaration terminated incorrectly

0
 
LVL 3

Expert Comment

by:jaba
ID: 1165901
check CLight (intt state ) {m_nLightState = state;};
to
CLight (int state ) {m_nLightState = state;};
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ljmiller
ID: 1165902
I will agree to increase points.
0
 
LVL 3

Expert Comment

by:jaba
ID: 1165903
I understand , why you BC compiler generating errors. You need to put class declarations into HEADER file ( noname.h) , when create noname.cpp and include noname.h into it :
#include "noname.h"

void main ()
{
CLight lt(CLight::S_RED);
lt.SetState(CLight:S_AMBER);
}


0
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 150 total points
ID: 1165904
It's perfectly legal to put class declarations in a .cpp file!
There is a lot of tinkering going on here!
You put them into a header file only if you want other modules (.cpp files) to share the class declaration.

First some remarks on the class CLight:
1. Since you use
   enum STATE { S_GREEN, S_AMBER, S_RED };
   you should not declare the light state-member as
   int m_LightState;
   but as:
   STATE m_LightState;
   to let the compiler help you finding possibly bugs, it will
   complain if you accidentally try to assign an int value to m_LightState

Here's how it should be: (You can put everything in one .cpp file, I did point out how to split it up in a .hpp (header) and
.cpp (module) file.

#include <cstring.h>
class Light
{
  public:
    enum State { Green, Amber, Red }; // the actual values don't matter!!
  private:
    State state; // The private variable to hold the State
  public:
    Light(State s) : state(s) {}
    void setState(State s)  { state = s; }
    State getState() const  { return state; }
    string asString() const;  // member function is only declared here
};

// member-function definition (actual code) in light.cpp file
string Light::asString() const
{
  switch(getState())
  {
    case Red    : return string("Red");
    case Amber  : return string("Amber");
    case Green  : return string("Green");
  }
  return string("Undefined State!");
}

// usage (main also in .cpp file, let say main.cpp)
#include <iostream.h>
#include <light.hpp> // not neccesary if you put everything in one file
int main()
{
  Light light(Light::Green);
  cout <<  light.asString() << endl;
  light.setState(Light::Red);
  cout << light.asString() << endl;
  return 0;
}

Use a project where you put your .cpp files.
Let me know if anything goes wrong.
.luc.
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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

670 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