-
March 21st, 2009, 09:02 AM
#1
classes question
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.
-
March 21st, 2009, 09:27 AM
#2
Re: classes question
You'll use classes for the OOP part of C++. As for classes vs structs, see this.
-
March 21st, 2009, 09:30 AM
#3
Re: classes question
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.
-
March 22nd, 2009, 12:14 AM
#4
Re: classes question
The difference between class and struct is only the access method.
Thanks for your help.
-
March 22nd, 2009, 01:31 PM
#5
Re: classes question
Thanks for the posts. And can I ask you what will I need the private: for? Is there any specific reason?
Thank you.
-
March 22nd, 2009, 02:12 PM
#6
Re: classes question
Originally Posted by StGuru
Thanks for the posts. And can I ask you what will I need the private: for? Is there any specific reason?
Thank you.
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.
-
March 22nd, 2009, 02:42 PM
#7
Re: classes question
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.
-
March 22nd, 2009, 01:42 PM
#8
Re: classes question
Originally Posted by StGuru
And can I ask you what will I need the private: for? Is there any specific reason?
You might want to read an introductory book like Accelerated C++ that will explain both the syntax and concepts to you, step by step.
I could try and explain to you, but do you know what are member functions?
-
March 23rd, 2009, 03:38 AM
#9
Re: classes question
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
-
March 23rd, 2009, 03:50 AM
#10
Re: classes question
Originally Posted by Abhishek Chauhan
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
What are you talking about? There's nothing* that the struct class-key can't do when compared to the class class-key.
*The only exception, of course, is the special treatment of the class keyword when it comes to template parameters.
-
March 23rd, 2009, 03:58 AM
#11
Re: classes question
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.
-
March 23rd, 2009, 04:06 AM
#12
Re: classes question
Originally Posted by Abhishek Chauhan
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.
-
March 23rd, 2009, 04:39 AM
#13
Re: classes question
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.
-
March 23rd, 2009, 07:51 AM
#14
Re: classes question
Originally Posted by Abhishek Chauhan
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.
Spell check. Use it. It's hard to figure out what you're trying to say.
-
March 23rd, 2009, 12:18 PM
#15
Re: classes question
I tried, and also I can use the public and private in the struct. If struct = class what are both for?
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
|