Quick C++ Help

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Tea Bag

Golden Member
Sep 11, 2004
1,575
3
0
Originally posted by: nife4
Originally posted by: DaveSimmons
It's too bad your book was glued shut, or you could have learned something from opening it.

If this is how motivated you are to learn, perhaps you should switch to a business degree.

http://shop.osborne.com/cgi-bin/osborne/0072232153.html


this book doesn't really teach anything about classes and structures... but yeah i got a general understanding of what was supposed to be taught to me last here by reading it.


I thought Schlidt's C++: The Complete Refrence OR Java: The Complete Refrence books were excellent: I still use the Java one quite frequently. I highly doubt that the one you have in your link "doesn't really teach anything.." Perhaps you should read that one, or heck - maybe you should read the one you have over again..
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: nife4
( C++ A Beginner's Guide, Second Edition - Herbert Schildt )
this book doesn't really teach anything about classes and structures... but yeah i got a general understanding of what was supposed to be taught to me last here by reading it.
I wasn't impressed by Schildt's C++: The Complete Reference, I threw away my copy to make room for better books.

But it's still up to you to learn, by getting another book from the library, going to office hours, working with other students, or whatever it takes.

If you have any thought of working as a software developer, you need to learn to learn on your own. A lot of the time all you'll have is a poorly-annotated SDK or API listing and whatever scraps of examples you can find on Google.
 

nife4

Senior member
Nov 24, 2003
375
0
0
Originally posted by: newbiepcuser
Originally posted by: HumblePie
private is to allow only the class the variable/attribute it belongs or is instantiated in to be accessed only by that class. It's mainly to prevent the corruption of data contained in that variable/attribute by randomly changes. This was an issue with pointers in C. Hence, getters and setters. If you need to change or retrieve data from a class, you use getters and setters.

If you understand this statement, then you can from here. Do you understand the concept of global? Do a search on google public vs private, it should help you get a run down on the importance of prevention of data corruption.

Whats done is done, you slack off. I've done it too and I took "hold my hand" professors etc and it I paid for it. I would say most CS professors sucks monkey balls and expect you do most of the work on your own. If you continue with CS, then you should expect to put more time into it.

There are also plenty of C++ forums with FAQs also on the net. They won't hold your hand either but there is some good reading.

I went off of this.... but yeah... i've read the section twice... time to get a different book...

and i actually do my work and study and sh!t, just been busy with moving and trying to get a vehicle to get to school... so i didn't have time to study for the test...
 

DaShen

Lifer
Dec 1, 2000
10,710
1
0
Suppose you were to use inheritence and add a child class. Well the private values for the parent class would not be used. The child class could then make a subprocedure of the same name but it would not access the parent class. This is to prevent corruption of data. The private classes allow for the developer to run procedures on the class without allowing the procedure to be publically called by the end-user but not by the developer. I believe that there may be a way to set a procedure as protected (although I could have easily messed that up with another language) so that all classes and derived classes could access the variable, but with a private value, you cannot access it but the class itself uses it.

Layman's Example:
Suppose you have a Class Ellipse. And you have private procedures that calculates the area, the height and width, and the circumference of the ellipse. But now you want to extend that class with Class Cicle (area and circumference should now be protected) while setting the height and width should be private. The public class would be the one to initialize the object and the other one would be the one to display the area and other calculated values.

A better implementation would be to set a Class called 2DObject with virtual classes (classes that need to be defined in any derived class) and therefore you won't run into any issues of messing with data that should be private.

Hope that helps. Next time, quit whining about your professor and just go to class and prioritize, man. You will see that this stuff becomes easy when you do that.
 

sao123

Lifer
May 27, 2002
12,656
207
106
Originally posted by: nife4
Briefly comment on the following statement: The main reason for using the private access specifier in a class is to "hide the data" from the user who is using the respective objects.

Also known as "BlackBox" programming.
The example is typically a black box with input & controls on one side, and output on the other. Like a vending machine(except it doesnt have the glass front). All you need to know is to put in the 2 quarters, push A4, and out pops a snickers bar. You dont have to know how it works inside to use it.
The idea is to separate the interface from the implementation, which promoted software reuse. This is the foundation for understanding how are used. I can write a dll which performs some function, and give you a header/library to explain the controls and output, and you can use it, without seeing my private code.
Privacy gives added security to ensure (me) that you cannot reach inside my black box and screw anything up, or cheat by not putting in the quarters and still getting something.
 

randumb

Platinum Member
Mar 27, 2003
2,324
0
0
Originally posted by: DaShen
Suppose you were to use inheritence and add a child class. Well the private values for the parent class would not be used. The child class could then make a subprocedure of the same name but it would not access the parent class. This is to prevent corruption of data. The private classes allow for the developer to run procedures on the class without allowing the procedure to be publically called by the end-user but not by the developer. I believe that there may be a way to set a procedure as protected (although I could have easily messed that up with another language) so that all classes and derived classes could access the variable, but with a private value, you cannot access it but the class itself uses it.

Layman's Example:
Suppose you have a Class Ellipse. And you have private procedures that calculates the area, the height and width, and the circumference of the ellipse. But now you want to extend that class with Class Cicle (area and circumference should now be protected) while setting the height and width should be private. The public class would be the one to initialize the object and the other one would be the one to display the area and other calculated values.

A better implementation would be to set a Class called 2DObject with virtual classes (classes that need to be defined in any derived class) and therefore you won't run into any issues of messing with data that should be private.

Hope that helps. Next time, quit whining about your professor and just go to class and prioritize, man. You will see that this stuff becomes easy when you do that.

afaik

in c++, protected can be accessed by only the class or subclasses

in java, protected can be accessed by the class, subclasses, and other classes in the same package. there used to be a private protected, which is the same as the c++ protected, but it was taken out of the language
 

nife4

Senior member
Nov 24, 2003
375
0
0
wow... thanks for the explaination from the last 4 people... that blackbox/vending machine example makes a lot of sense... thanks...