Struct and Classes, whats the difference ?
Hi,
I''ve just discovered that structs can have functions, So my question
is: What is the difference between structs and functions ???
And, by the way, What is virtual functions i.e. "virtual A_Plus_B()" ?
Kamikaze
In C++, the only difference between a struct and a class is that structs default to public visibility and public inheritance, while classes default to private visibility and private inheritance. Other than that, they are exactly the same.
A virtual function is one which can be overridden in a derived class. Suppose I have a class A, with a virtual function called foo(). I then derive class B from class A. Class B can now provide its own implementation of foo(), if it so desires.
A virtual function is one which can be overridden in a derived class. Suppose I have a class A, with a virtual function called foo(). I then derive class B from class A. Class B can now provide its own implementation of foo(), if it so desires.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
I''ve understood about the classes and the structs, however I
didn''t understand about the virtual functions...
Kamikaze
didn''t understand about the virtual functions...
Kamikaze
If a function is not virtual, the version of the function called depends on the static type of the object, that is, the type of the pointer/reference. If the function is virtual, the version of the function that gets called depends on the dynamic type of the object, that is, the type you provided to new.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | Free C++ IDE. ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Boost C++ Lib ]
[edited by - Fruny on December 10, 2002 11:20:58 PM]
#include <iostream>class Foo{public: void func1() { std::cout << "1- Foo" << std::endl; } virtual void func2() { std::cout << "2- Foo" << std::endl; } virtual ~Foo() { // The class needs a virtual destructor so that // delete destroys the object based on its // dynamic type (matching the call to new) and // not based on the static type of the pointer // holding the object (which falls into 'undefined // behaviour' - silently generates invalid code). }};class Bar : public Foo{public: void func1() { std::cout << "1- Bar" << std::endl; } virtual void func2() { std::cout << "2- Bar" << std::endl; } // base Foo's destructor is virtual, so will be Bar's};int main(){ Foo* a = new Foo; // static type - Foo, dynamic type - Foo Foo* b = new Bar; // static type - Foo, dynamic type - Bar Bar* c = new Bar; // static type - Bar, dynamic type - Bar std::cout << "1- is non virtual - uses the type of the pointer" << std::endl; std::cout << "2- is virtual - uses the type of the object" << std::endl; a->func1(); // prints "1- Foo" - the pointer is a Foo* a->func2(); // prints "2- Foo" - the object is a Foo b->func1(); // prints "1- Foo" - the pointer is a Foo* b->func2(); // prints "2- Bar" - the object is a Bar c->func1(); // prints "1- Bar" - the pointer is a Bar* c->func2(); // prints "2- Bar" - the object is a Bar delete a; // destructor is virtual, delete a Foo, ok delete b; // destructor is virtual, delete a Bar, ok delete c; // destructor is virtual, delete a Bar, ok};[/quote]
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | Free C++ IDE. ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Boost C++ Lib ]
[edited by - Fruny on December 10, 2002 11:20:58 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny, could you remake your code please ? It''s a bit
confusing, you should be printing different strings so I
could know if it''s printing the virtual function or the
non-virtual function...
And I suppose you mean "Foo* a = new Foo" right ?
Kamikaze
confusing, you should be printing different strings so I
could know if it''s printing the virtual function or the
non-virtual function...
quote:
Foo* a = new Few
And I suppose you mean "Foo* a = new Foo" right ?
Kamikaze
Feh, that''s something you could have do yourself... but well, anyway, it''s done.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | Free C++ IDE. ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Boost C++ Lib ]
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | Free C++ IDE. ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Boost C++ Lib ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement