Log in

View Full Version : C++



hoshimoshi
06-25-2012, 03:18 AM
Hi I'm learning C++, my goal is to go through the youtube tuts first, "Thenewboston", "lets learn c++", and too read at least 2-4 books by September, I've bought several books, and found a few useful ebooks (I think). I want to go through all of this before fall classes start :].

I'm going through the tuts, and I understand them enough to not "have" to replay them over, but I ran into this one tut. So pretty much he was teaching the proper way to create classes, and set them inside of obj, so they could be access by calling them. But he teaches us not to use public, but to use private. Why exactly is this?

I understand public classes can be access by the main function easily, but why go through the hassle of setting private classes and creating a public function within the private class so that the main function can access it?

I ask this because since Java is Object orientated, and you guys are already good at it, what benefits does it have for c++?

Also I'm seeing alot of comments stating that setting private classes are a waste and just to write it within the main function. Does this hold truth?

weequ
06-25-2012, 03:24 AM
I think it's just easier to develop/maintain and less error for bugs if you keep the functions and variables that you don't need outside of the class private.

Never coded in c++ though.

Brandon
06-25-2012, 04:17 AM
Well I've written a ton of C++ tutorials one of them being on Templated Classes here: http://villavu.com/forum/showthread.php?t=82610

Now don't bother trying to understand it if you are just starting out as it's going to probably discourage you BUT lets start from the beginning.

There is no such thing as a "Private Class". The correct terminology is Private/Public/Protected Inheritance.

There is a reason we call it that.

One: Private members of a class can only be accessed by other members of the same class (Brothers/Sisters, No cousins or Friends).
Two: Public members can be accessed by anyone (functions, main, etc) and any class or object.
Three: Protected members can only be accessed by other class members OR by classes that are "Friend"/"Inherited".
Four: Friends. Friends can access any part of your class, private, public and protected members. So limit your friends.
Five: Classes cannot be public; Only members can. Thus any class can access your class/object automatically. This is why we set members public/private/protected.
Six: In Java it's something like Public Class B Extends A or whatever. In C++ it's Class A : B; That's called deriving/inheriting.
Seven: Nested classes can put an accessor on It's daughter/son class.

Example:


class A
{
protected:
class B
{
};
};


I believe that's the only way to simulate what I think you're thinking..

Now why do we make things private? well we don't want the user to be able to change values of private members from outside the class. Here we can force them to use a Getter/Setter instead.

Example: Sometimes our class may have a pointer member or an internal member that will hold data. Other members/data may depend on the values that the private member holds. Now if the private member was public, and a user changed it, it can mess up the other members/functions.

Code Example:

class TBitmap
{
private:
RGB *ArrayOfPixels;

public:
TBitmap(int Width, int Height); //Constructor for our class.
~TBitmap(); //Destructor.

void SetPixels(int Index, COLORREF Colour); //This is known as a setter function.
};

TBitmap::TBitmap(int Width, int Height) : Pixels(0) //Implementation of the constructor.
{
//Create A Blank Bitmap here.
ArrayOfPixels = new RGB[Width * Height]; //Every new[] allocation must have a delete[] clearance.
}

TBitmap::~TBitmap() //Implementation of the destructor.
{
delete[] ArrayOfPixels; //We MUST Cleanup after ourselves.
}

TBitmap::SetPixels(int Index, COLORREF Colour)
{
ArrayOfPixels[Index] = Colour; //Our user never gets to touch the array. We let a function do the work.
//Thus the user accesses this indirectly!
}


int main()
{
//Let us ASSUME that ArrayOfPixels was public ok?

TBitmap SomeBMP(100, 100); //Create a bitmap of 100 x 100 pixels.
delete[] SomeBMP.ArrayOfPixels; //DANGEROUS!! We deleted our array from outside the class.
//Our program ends here BUT it doesn't just end, it will crash or cause undefined behaviour!
//WHY? Because we already deleted ArrayOfPixels and our Destructor is about to get called to delete something that isn't there since we already deleted it!!!!
//For this reason it was better to leave ArrayOfPixels as private OR use a MoveConstructor/CopyConstructor/C++11 Semantices.
//Also for this same reason is why we access it through the Setter functions so the user can't bullshit us. We control how they use our class!
}


Read the comments in the above code. As you can see, it CAN be dangerous to expose some members as public without the proper semantics.

By Default, All class members are private and all Struct Members are public. So inother words a Struct is what you are calling a public class and a class is what you are calling a private struct.

Both are the same thing with different default inheritance. Think from the user standpoint when creating a class such as the above. We don't want users doing stupid things such as touching and deleting things they shouldn't.

Anyway here is someone asking the same thing as you but with more beginner friendly answers: http://stackoverflow.com/questions/4792614/making-classes-public-to-other-classes-in-c

I would recommend that you read that to see the differences between C++ and Java/C# class inheritance.

LordJashin
06-25-2012, 07:04 PM
Hi I'm learning C++, my goal is to go through the youtube tuts first, "Thenewboston", "lets learn c++", and too read at least 2-4 books by September, I've bought several books, and found a few useful ebooks (I think). I want to go through all of this before fall classes start :].

I'm going through the tuts, and I understand them enough to not "have" to replay them over, but I ran into this one tut. So pretty much he was teaching the proper way to create classes, and set them inside of obj, so they could be access by calling them. But he teaches us not to use public, but to use private. Why exactly is this?

I understand public classes can be access by the main function easily, but why go through the hassle of setting private classes and creating a public function within the private class so that the main function can access it?

I ask this because since Java is Object orientated, and you guys are already good at it, what benefits does it have for c++?

Also I'm seeing alot of comments stating that setting private classes are a waste and just to write it within the main function. Does this hold truth?
I learned from this site - http://www.cplusplus.com/doc/tutorial/
Helps a lot for reference too.

wantonman
06-26-2012, 04:26 AM
dude c++ is the easiest to learn man especially is you watch this guys noob c++ videos there is nothing you can miss with this... it s very boring and pretty much a college lecture but this is the fastest way to learn here it the youtube link if you haven't all ready discovered this guy...
https://www.youtube.com/user/antiRTFM

Brandon
06-26-2012, 05:22 AM
dude c++ is the easiest to learn man especially is you watch this guys noob c++ videos there is nothing you can miss with this... it s very boring and pretty much a college lecture but this is the fastest way to learn here it the youtube link if you haven't all ready discovered this guy...
https://www.youtube.com/user/antiRTFM

Until Templated Abstracts and Regex's make you go mad or even Embedded ASM to hide underlying features such as a messagebox from OllyDBG/reverse engineering or Running a DLL/Exe from memory only. I wouldn't exactly call C++ the easiest to learn. It does have it's stepping stones though. I've never actually seen that guy's videos but the diagrams look pretty damn nice.