What's the point of a struct? I mean, isn't it basically the same thing as a class? So what it's used for?
Yeah, it is basically the same thing as a class. Keeping 'struct' in the language kept C++ a near-superset of C, since C contained structs. The real mystery to me is why they added 'class' in the first place. I guess it was to make the language seem more object-oriented.
Thanks guys. So the only difference is that members of a struct are public, and members of a class are private...
>So the only difference is that members of a struct are public, and members of a class are private...
Inheritance defaults to private for classes and public for structures too. Just remember that the default access for classes is private and for structures it's public.
Thanks. I have one more question:
In one of my books it was demonstrating a base class a derived class. In the base class the members were made protected....why would you do something like that? I know that when you have protected access it gives the derived class access to it, but doesn't this break OO style? Shouldn't those members of the base class be private but have accessors and mutator methods?
protected data members are virtually a no-no. what normally goes into the protected section are member functions, especially virtuals which may need to be called from derived classes but not necessarily the outside world.
>I know that when you have protected access it gives the derived
>class access to it, but doesn't this break OO style?
By members I assume you mean data members and not member functions. Protected access is a sneaky beast, because if you have protected data members then someone only has to derive from your class to legally access the internals. A lot of people (including me) believe that this is a travesty and anyone who attempts it should be shunned and ridiculed. Protected member functions are just fine though.
The guidelines for safe inheritance are simple:
1) All data is private
2) The public interface is for everyone
3) The protected interface is for derived classes
4) Prefer abstract base classes
5) Inheritance hierarchies should be broad and flat, not thin and deep
That covers your ass when it comes to access and it helps avoid annoying inheritance issues.
I believe I'm getting somewhere now, just got to get all these weird questions answered!
Isn't it about time forums rewarded their contributors?
Contribute to this discussion and earn rewards points that can be cashed out for dollars.
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Here is the sample output that I ...