-
March 23rd, 2009, 01:25 PM
#16
Re: classes question
Classes can be used for polymorphism and abstraction. Structs can not, that's the biggest difference.
Anything you can do with OOP you can do with procedural programming. There is nothing that you can do with classes that you can't do with C and structs. The difference is how easy it is to code.
Last edited by ninja9578; March 23rd, 2009 at 01:27 PM.
-
March 23rd, 2009, 01:26 PM
#17
Re: classes question
It's a legacy thing mainly. In C, structs were just collections of data. C++ introduced classes, which represent objects which have data and a defined way of interacting with it.
As it happened C++ also upgraded structs to have all the same capabilities as classes with just a few minor differences which have been covered.
So essentially they're the same thing. However, in many cases structs will be used like the old C structs----just to hold data, nothing more. Classes are more often used if a fully-encapsulated object is truly desired. It's tradition more than anything else.
Originally Posted by ninja9578
Classes can be used with polymorphism. Structs can not, that's the biggest difference.
Structs work just fine with polymorphism in C++.
One advantage of structs is that "struct" is a keyword in C as well as C++. So if you want to write a module in C++ which is callable from C, you can forward declare the struct type in the header, and then the C program can carry around pointers to your object even though it can't correctly interpret them; essentially they'd just be opaque handles as far as C is concerned. But it can do it, and in a type-safe manner (rather than relying on void*). That's not true for classes, since the "class" keyword doesn't exist in C.
Last edited by Lindley; March 23rd, 2009 at 01:30 PM.
-
March 23rd, 2009, 01:37 PM
#18
Re: classes question
Originally Posted by StGuru
I tried, and also I can use the public and private in the struct. If struct = class what are both for?
Conventionally, the struct keyword would be used to define a class that is nothing more than an aggregate of some variables, without any behaviour specified by member functions. Sometimes, I use it to define function objects that only have one member, namely the public overloaded operator().
Originally Posted by ninja9578
Classes can be used with polymorphism. Structs can not, that's the biggest difference.
Here is a counterexample:
Code:
#include <iostream>
#include <memory>
struct Abc
{
virtual ~Abc() {}
virtual void print() const = 0;
};
struct X : Abc
{
virtual void print() const
{
std::cout << "I am an X." << std::endl;
}
};
struct Y : Abc
{
virtual void print() const
{
std::cout << "I am a Y." << std::endl;
}
};
int main()
{
std::auto_ptr<Abc> p(new X);
p->print();
p.reset(new Y);
p->print();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|