Hello! I got one quick question. For what tasks will I use classes? Why classes and not structures?
Please give me example, where I can solve the task only with classes and not structures.
Thanks in advances.
Printable View
Hello! I got one quick question. For what tasks will I use classes? Why classes and not structures?
Please give me example, where I can solve the task only with classes and not structures.
Thanks in advances.
You'll use classes for the OOP part of C++. :) As for classes vs structs, see this.
struct and class are identical in every way, except that when declaring methods and/or data members in a class, by default, they are treated as private members of the class. In a structure, by default, all methods and/or data are public.
Naturally, you can change the permission levels of methods and data members using the appropriate label of "public:", "protected:", or "private:".
Many s/w developers that program in C++ tend to use a class vs. a struct for modeling their objects. When only data members need to be encapsulated is a struct used. But that is just based on developer's preference.
The difference between class and struct is only the access method.
Thanks for the posts. And can I ask you what will I need the private: for? Is there any specific reason?
Thank you.
You might want to read an introductory book like Accelerated C++ that will explain both the syntax and concepts to you, step by step.Quote:
Originally Posted by StGuru
I could try and explain to you, but do you know what are member functions?
Mostly to ensure the safety and proper use of your class. As a really simple example, say your class has a data member that can only store a value between 1 and 10. Making that member private ensures that other users of that class can't set an inappropriate value. You'd make a method to set the value public and have it reject inappropriate values.
Think of it this way. You've got this big, complicated algorithm. It's frankly ugly, and relies on all sorts of assumptions about the input data. Trying to incorporate something like that into a larger bit of code would be pretty daunting-----who knows what might break it.
But, if you can make all that functionality class-private, and throw a simple, clean, easy-to-understand interface on the class.....suddenly all the messiness is hidden from the rest of the code. It's abstracted away, and all you have to worry about is validating arguments to the interface. It makes everything a lot easier.
Most of the people talk differentiate classes and structure on basis of setting varriables etc as public or private by default.
the thing here to understand is that procedures can be calles , used whatever in class but it can't be used in structures.
Representation of data might seem easy in structure but when it comes to utilizing there are lot of limitations and use of classes can really help you alot in the long run
1. In classes the default scope is private whereas in structures u have public.equir
2.Also u can define the scope according to is requiremeuctunts in claasses.
3. In classes u can create fn nd methods which is not so in d case of structures.
4.You cn also do operator overloading in case of classes which is not so in d case of structures.
5.Various other object - oriented features are found in classes and not in structures wherin u can create objects which is not possible in structures.
- Correct. Don't forget about inheritance as well.
- What are you trying to say?
- Wrong.
- Wrong.
- Wrong.
In short, see my previous post.
Hello Abhishek Chauhan
I think you understood OP's confusion but not what's been said here.
I do too think that when the OP asked "why structure" in place of "class",
he was actually thinking about the struct in C, and not the struct in C++
In C++, the keyword struct and class are used interchangeably,
the difference of which is what's been said by Plasmator and others.
For example, some use the keyword struct for POD type,
so it really is a matter of perference.
You could do a simple foo class using struct and compile with C++ compiler (not C compiler).
to see this for yourself.
I tried, and also I can use the public and private in the struct. If struct = class what are both for?
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.
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.
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.
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().Quote:
Originally Posted by StGuru
Here is a counterexample:Quote:
Originally Posted by ninja9578
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();
}