-
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.
-
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.
-
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; ).
-
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();
....
:eek: