classes vs structure
-
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
classes vs structure
in all my programs weather it games or simulations that depends mainly on OOP i didn't find the need of using a class but i found that using structures and independent functions are more fast easy and organized....
so simply what's the use of classes are they just a structure with functions included??
so simply what's the use of classes are they just a structure with functions included??
-
- Competition winner
- Posts: 1123
- Joined: Sun Jun 10, 2007 11:14 pm
-
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
All he is saying is that the only difference between a class and a struct is the default protection level. You say that in all of your code you use struct instead of class, but i think you fail to realize their similarities.
The following are supposed to be exactly the same.
The following are also the same...
Also, just because you are using an object-oriented language and you declare your own types does not make your code object-oriented.
Travis
The following are supposed to be exactly the same.
Code: Select all
struct A {
int a_;
};
void do_something_with_A(A* pa)
{
pa->a_ = 1;
}
class B {
public:
int b_;
};
void do_something_with_B(B* pb)
{
pb->b_ = 1;
}
Code: Select all
struct A {
private:
int a_;
public:
void do_something();
};
void A::do_something()
{
this->a_ = 1;
}
class B {
int b_;
public:
void do_something();
};
void B::do_something()
{
this->b_ = 1;
}
Travis
You can do everything with structs and independent functions - as that is what you do in c-programming. In c++ structs and classes are the same (except the default protection), but they are used different. The difference lies more in the way you think about them than in the way they are actually different to the compiler. C++ programmers use structs usually when they need dumb blocks of data with no intelligence on their own. Classes on the other hand are always caring about their own data and even try to hide the data so you only interface with objects through defined functions.
Another thing you won't get so easy by using independent functions with structs is the OO stuff (you can still do it, but it's harder). Memberfunctions can be overloaded and they can be virtual - you can't do that with independent functions without first coding an own OO system.
Another thing you won't get so easy by using independent functions with structs is the OO stuff (you can still do it, but it's harder). Memberfunctions can be overloaded and they can be virtual - you can't do that with independent functions without first coding an own OO system.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
It's no difference for that example. But it is a difference for the other cases I mentioned. Virtual functions and overloading. And it is a difference in thinking about the code. c does emphasize working with data while in c++ you think about working with objects and try to hide the data.
To allow the bark(dog) thing the dog has to open up all it's internal data. Which means everyone can affect it. There might be a howl(dog) function anywhere in your code not even knowing about the bark and they might conflict. While in dog.bark() you can hide data and you know every function still accessing the dog is also in the dog class or is using a defined interface. So it gives you stronger encapsulation and that allows you to find problems a lot faster because you don't have to look all over the place.
It is not impossible to write oop code in c, it is just harder.
To allow the bark(dog) thing the dog has to open up all it's internal data. Which means everyone can affect it. There might be a howl(dog) function anywhere in your code not even knowing about the bark and they might conflict. While in dog.bark() you can hide data and you know every function still accessing the dog is also in the dog class or is using a defined interface. So it gives you stronger encapsulation and that allows you to find problems a lot faster because you don't have to look all over the place.
It is not impossible to write oop code in c, it is just harder.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Maybe an example for illustration:
Your code doesn't have to care whether animal is cat, dog or even an altogether different animal. This line makes sure the correct call method for the animal gets called. This is called polymorphism. (coincidentally a similar example is used.)
There are many advantages to this. Mentioned data hiding is one, but also it allows for the open/closed principle. One of the prime OO principles.
You cannot easily simulate this behavior with top-level functions.
One solution was to have a switch in a function like this:
The disadvantage is apparent immediately. If you introduce a new Animal, you have to change this function in order to allow new types of Animals. This makes the code hard to maintain and extend.
An actual Irrlicht example would be S3DVertex and all the switches over E_VERTEX_TYPE.
Code: Select all
class Animal
{
public:
virtual void call() = 0;
}
class Dog : public Animal
{
public:
virtual void call()
{
std::cout << "woof";
}
}
class Cat : public Animal
{
public:
virtual void call()
{
std::cout << "meow";
}
}
Animal* animal = createAnimal();
animal->call();
There are many advantages to this. Mentioned data hiding is one, but also it allows for the open/closed principle. One of the prime OO principles.
You cannot easily simulate this behavior with top-level functions.
One solution was to have a switch in a function like this:
Code: Select all
makeAnimalCall(Animal* animal)
{
if (animal->isType("Dog"))
{
std::cout << "woof";
}
else if (animal->isType("Cat"))
{
std::cout << "meow";
}
else if(...)
...
}
An actual Irrlicht example would be S3DVertex and all the switches over E_VERTEX_TYPE.
-
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
Waow, thanks for the explanation, I did not know it was possible to make a function insided a struct !
One more thing, why do you have to put the keyword public when writing:
I get the protected, private and public concepts, but why does polymorphism has to deal with public concept ?
One more thing, why do you have to put the keyword public when writing:
Code: Select all
class dog : public animal { etc };
If you write "private animal" (Grrr!) then any class inheriting from "dog" won't be able to access animal.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
It's as if you flagged every members and member functions within animal as private, no matter whether they were public or protected before. However, you can only protect more, not less when you inherit, hence a private member will never be accessible from an inherited class (well, shouldn't, there's way to mess things up, but you really shouldn't), and a protected can only become private or stay as it is.
http://www.parashift.com/c++-faq-lite/p ... tance.html
http://www.parashift.com/c++-faq-lite/p ... tance.html