Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Will someone please give me an example of a Working class!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hi I have a big problem with classes. I am wondering if someone could just show me a simple example of how to use a class and implement it. Let's say this..You want to write a header file (.h) with the class information. Then you want to implement the .h file (where the class is declared) in a .cpp file or .cc file (the main portion of the program) Let's say you need two variables which are declared in the class int a, int b. and these two integers are than CIN my the user in the main portion of the program. The two numbers are then added, subtracted and multiplied together in a seperate function, let's call it double MathPortion. Thank you
 
this will answer very little
try Teach Yourself C++ in 21 days @

//////header.h
class Cheader
{
private:
int a, b;
public:
int geta() { return a; }
int getb() { return b; }
void seta(int A) { a=A; }
void setb(int B) { b=B; }
double MathPortion(int A, int B) { ....;}
}
///////usesheader.cpp
#include "header.h"

Cheader theheader;
double answer;

main()
{
// or Cheader theheader;
header.seta(5); header.setb(6);
answer = MathPortion(header.geta, header.getb);

}
 
Try to think of a class an object definition. An object can have data and three other things. Events, methods, and properties.
An event is something that happens to an object of that class. Example: Class Dog. An event of that class might be called Dog_Gets_Hit_By_A_Car.
A property is something that describes the object. Example: Class Dog. A property might be that its brown in color.
A method is a function that the object interacts with. Example, Class Dog: A method might be bark(). (Causes the dog to bark.) I hope that helped, now heres another example of how classes can work together:

Heres a generic example of classes in pseudocode:

Class1: Food class

property is_edible; (I told ya it would be generic)

Class2: Fruit class
[Inherit Food class] -> this means it inherits all the properties of the Food class... (methods too)

property grows_on_plants;
property has_seeds; (Assuming all fruit had seeds)

Class3: Apple Class
[Inherit Fruit class] // No need to inherit food class,
// because Fruit already does that
property color; // possible values, brown, white, etc
method eat_me(); // executed when something eats the apple
event fall_from_tree();


---------------------------------------------
I hope that helped. It took me a while to really understand
the class structures, and I'm still not extremely comfortable using it, but I think I understand it a lot better since I got some generalized examples like these.

MG
 
THe header file is used to declare what tyhe class is and what methods are available. The cpp file is used to instantiate the class; that is write the code for each of the methods declared in the header file.

// SApplication.h

#include <iostream.h>
#include <string>

using namespace std;

class SApplication
{
public:
SApplication();
virtual ~SApplication();

void Run(void);
private:
string m_name;
}

// SApplication.cpp

SApplication::SApplication( string& name )
: m_name( name )
{
}

SApplication::~SApplication()
{
}

void SApplication::Run(void)
{
cout << &quot;THe application &quot; << m_name
<< &quot; as started&quot; << endl;
}

// MyApplication.cpp

#include <iostream.h>
#include <string>
#include &quot;SApplication.h&quot;

using namespace std;

int main()
{
int status = 0;
string applicationName(&quot;Application Tester program&quot;);
SApplication myApp( applicationName );

myApp.Run();

cout << &quot;Goodbye&quot; << endl;
return status
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top