Page 1 of 1

C++ Classes

Posted: Wed Jun 02, 2010 1:59 pm
by VolTeK
i am wondering if i can use the same class template over and over again, for example

class vehical
{
unsigned int theOwner;
drive();
}

could i do this:

vehical ford;

and then make another
vehical chevy; ? that would use two different classes both the same, or just rename it?

Re: C++ Classes

Posted: Wed Jun 02, 2010 2:02 pm
by Creature
GhostXoPCorp wrote:i am wondering if i can use the same class template over and over again, for example

class vehical
{
unsigned int theOwner;
drive();
}

could i do this:

vehical ford;

and then make another
vehical chevy; ? that would use two different classes both the same, or just rename it?
Well firstly your drive() member function doesn't have any return type, so the compiler will most likely produce an error. You also finish the class definition by a trailing ';', so class Name {};. I also think you meant Vehicle instead of Vehical :P. The point of structures/classes is to define objects from them, so I don't see why you shouldn't be able to use the same class over again:

Code: Select all

class Vehicle
{
public:
   void foo() { std::cout << "Hello foo()\n"; }
};

int main()
{
   Vehicle Opel;
   Vehicle Ford;

   Vehicle BMW, Toyota;

   Ford.foo(); // Calls 'foo' member function.
}
And so on. Does that answer your question?

Re: C++ Classes

Posted: Wed Jun 02, 2010 2:45 pm
by VolTeK
thank you, so in a class could i have


Code: Select all

int function() {
cout << "Example" << endl;
return 0;
};

class pizza
{
function();
}; 
pizza pepperoni;
and then to call the function:
pepperoni.function();

or would i have to set the class up like this:

Code: Select all

class pizza
{
int function(); //or void?
}; 

Re: C++ Classes

Posted: Wed Jun 02, 2010 4:32 pm
by Gigasoft
You must specify a complete prototype including return type when declaring any function.

When you define the function, you must prefix the name with the class name followed by two colons, unless it is defined inside the class definition block.

The primary advantage of classes is that they can be subclasses of other classes, and provide different implementations of the same functions.

Re: C++ Classes

Posted: Wed Jun 02, 2010 4:41 pm
by gravaera
Wow: this is nice. I like this valuable, teaching thread. I like how on an OS Development forum, where people are expected to be kernel developers, there are people who are asking how classes in C++ work. It's great reading, and it stimulates my mind. Nice to see that the community is moving forward.

Re: C++ Classes

Posted: Wed Jun 02, 2010 5:14 pm
by VolTeK
00000000000000000000000

Re: C++ Classes

Posted: Wed Jun 02, 2010 6:44 pm
by Gigasoft
Well, gravaera's right, I don't really see what a 13-year old with a bad attitude is doing hanging out at a forum for OS developers. You've even stated in another thread that you're here to start fights with anyone who is critical of you.

Re: C++ Classes

Posted: Wed Jun 02, 2010 7:43 pm
by VolTeK
0000000000000000000000000000000

Re: C++ Classes

Posted: Wed Jun 02, 2010 8:02 pm
by neon
This is the General Programming forum not the OS Development forum and thus is in the correct place.

Re: C++ Classes

Posted: Wed Jun 02, 2010 8:20 pm
by VolTeK
Exactly.

Two sub forums:

Operating system development\Everything else

Ull never guess where i posted this