C++ Classes

Programming, for all ages and all languages.
Locked
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

C++ Classes

Post 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?
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: C++ Classes

Post 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?
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: C++ Classes

Post 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?
}; 
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: C++ Classes

Post 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.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: C++ Classes

Post 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.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: C++ Classes

Post by VolTeK »

00000000000000000000000
Last edited by VolTeK on Wed Jun 02, 2010 8:10 pm, edited 1 time in total.
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: C++ Classes

Post 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.
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: C++ Classes

Post by VolTeK »

0000000000000000000000000000000
Last edited by VolTeK on Wed Jun 02, 2010 8:11 pm, edited 1 time in total.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: C++ Classes

Post by neon »

This is the General Programming forum not the OS Development forum and thus is in the correct place.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: C++ Classes

Post by VolTeK »

Exactly.

Two sub forums:

Operating system development\Everything else

Ull never guess where i posted this
Locked