I dropped classes, advanced explanation?

May 19, 2014 at 4:02pm
I am a self-learner of C++, currently, I know some previous programming languages well, but never got into classes. The book I'm reading about C++ - 1214 pages - talks very unclearly and shortly about classes, so I don't really get it. I hope you give an advanced explanation, giving the official terms of everything,
telling me the why behind things. Also consider I already know this:

I have already studied structs; I think it's just a struct with private, public and protected members and it can also have member functions?

I'm also totally lost out on constructors and destructors, and don't have a clue about ADT or Abstract Data Type (what the hell is that?)...
Note
Please avoid "Google it" answers because my case is very specific. I also want practical advice to the use of classes... Other than user-defined header files?

Last edited on May 19, 2014 at 4:04pm
May 19, 2014 at 4:38pm
I have already studied structs; I think it's just a struct with private, public and protected members and it can also have member functions?
Structs can have private and protected members too. And it can have member functions and constructors/destructors.

Only difference between class and struct is that class defaults to private members and private inheritance, and struct defaults to public ones.


Question you askes is very wide and it is not possible to fit everything in single post.
I would suggest that you read more about classes here: http://www.cplusplus.com/doc/tutorial/ and here http://www.learncpp.com/
May 19, 2014 at 7:37pm
std::string is an abstract data type. An ADT is just a data type created using the basic data types: bool, char, int, and float. std::string is an array of chars.

A constructor is a function that initializes your class and a destructor is a function that deletes your class.

for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class example
{
	public:

		example(); // constructor
		~example ( ); // destructor
		// other class functions would go here

	private:

		int value;
};

example::example ( ) // this function is called whenever someone creates an object of this class
{
	value = 5; // initialize our value to 5
}

example::~example ( ) // this function is called whenever an object of this class goes out of scope
{
	// since an int is a basic data type, we don't have to do anything here
}
May 20, 2014 at 3:51am
Thanks, now I get it... But what is so special about a class if structs also have private and protected members too. And can have member functions and constructors/destructors. Inheritance and composition??! That's all the fuss about classes?
May 20, 2014 at 4:27am
struct is a class. Not other way around.
keyword struct and specific behavior were left in C++ to make it compatible with C
Topic archived. No new replies allowed.