first class try

This is a discussion on first class try within the C++ Programming forums, part of the General Programming Boards category; howdy, this is my first experience with class objects. i created this class however i have some questions about why ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    first class try

    howdy,
    this is my first experience with class objects. i created this class however i have some questions about why it works.
    by pure luck i got the "GetName()" and "SetName()" functions to work by adding the pointer ref's - something i saw in a book - but i dont know what they (the pointers) do.
    in the "Label()" function i can name objects but i dont think i am creating objects. how would i create multiple objects of this class from user input.
    this is my first try with classes so any other suggestions would be appriciated.



    Code:
    #include <iostream.h>
    #include <math.h>
    
    using namespace std;
    
    
    class SBeam
    {
    public:
      char SBeamName;
      SBeam();
      ~SBeam();
      char* GetName();
      char* SetName(); 
      char Label();
    
    private:
     int Depth();
     char ItsName[6];
      int SbeamDepth;
      int depth;
    };
    
    SBeam::SBeam()
    {
    }
    
    SBeam::~SBeam()
    {
    }
    
    char* SBeam::SetName()
    {
      cout<<"Enter Beam Name: \n";
      cin.getline(ItsName, 6);
      Depth();
      return ItsName;
    }
    
    
    char* SBeam::GetName()
    {
      return ItsName;
    }
    
    
    int SBeam::Depth()
    {
      cout<<"Enter Beam Depth: ";
      cin>>depth;
      return depth;
    }
    
    char SBeam::Label()
    {
      cout<<"The Beam Label: "<<GetName()<<", "<<depth<<"!\n";
      return 0;
    }
    
    int main()
    { 
        SBeam B1;
        B1.SetName();
        B1.Label();
       return 0;
    }
    Thanks
    M.R.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    381
    I'm not sure if I get your post.
    Firstly what does you code do.
    And what do you mean by not sure that you are creating objects. The only actual object in your code is B1. Creating more would be just adding identifiers to that line.

  3. #3
    Unregistered
    Guest
    In your code, only one object ( B1 ) is being pushed on the Stack. The SetName() method, does not have to return a char*, since all you are trying to do is set the name. Make SetName() methods return value void.

    Calls to the Label() method are only to the B1 SBeam object.

    To Create objects "on the fly" requires Heap allocation with object pointers ( e.g. SBeam * B1 = new SBeam; ).

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    creating objects

    You are indeed creating objects... well at least one anyway.

    The line...
    SBeam B1;
    creates an object of type SBeam...

    if you want more than one object of this type you could try
    SBeam B1, B2, B3....B100;

    or

    SBeam B[ 100 ]; //then you can loop trough them

    of course the dynamic implementation also works but has its pitfalls...

    SBeam *pBeam;

    pBeam = new SBeam[ 100 ];

    pBeam[ 0 ].SetName();
    pBeam[ 0 ].Label();

    ....
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21