Classes, and structs

Is there some one who can explain classes and structs to me a little better?
nobody explains any of this in lamens terms. along with one other thing i dont quite understand which is void.
Last edited on
Ok:

Class and structs are basically the same, except Classes are private by default, whereas structs are public by default.

Classes/Structs are basically "objects" that contain variables inside of them.

EX:
1
2
3
4
5
struct cat {
  bool hasfur;
  bool hastail;
  string name;
}


Is sort of like:

A "cat" can have fur, a tail, and a name.

Cat
|
+---+---+
/ | \
fur tail name


There is other stuff, like classes containing functions, but once you understand the basic theory, it should be easy to understand the other stuff.


void is basically nothing. I.e.:

1
2
3
4
5
6
7
8
9
10
11
//this
void myfunc(void) {
  cout<<"This func takes no parameters and returns nothing";
  return /*nothing*/;
}

//is the same as
myfunc() {
  cout<<"This func takes no parameters and returns nothing";
  return /*nothing*/;
}
Tyvm, so essentially, classes jus hold variables? i mean, if i could jus put those variables into the main code, why would i need to use a class at all?

it should amount to nothing which again poses the question, what are voids ever used for if, no matter what they just return nothing in the end.

voids are used for better organizing code for reuse. You may want to print a huge glob of information from files, but you don't need anything to return from that function. Sure you could use an int return for success or something, but why bother. Thus you use a void function to print the information.

It's either that, or put in the full code EVERY time you want to do something like that. Which is a VERY bad idea.
OOO i see, ty jpeg :D so the only other question i have is about classes, what should i ever need to use one for and exactly how do you use them? i kno how to start a class and define certain variables in that class, its things like this i dont fully understand:

1
2
3
4
5
6
7
8
class orville
{
    int orville;
     orville(): //constructor right?
     ~orville(): // deconstructor right?
     orville::orville() //confused here
     int orville::~orville://confused here to
}



i kno its not how to write a class exactly but theres those things im still very confused about and dont know how to use. thats all i want to kno and tyvm to jpeg and huge thanks to firedraco for your help :)

Last edited on
Ok, well the constructor/deconstructor are "correct" need a ';' not a ':', but you can't access them inside of the class itself...

Basically, what you are talking about is the next part, functions inside of classes. This sort of leads into inheritance and stuff, but we will start out with the basic stuff:

1
2
3
4
5
6
7
8
9
10
11
12
class MyClass {
   int integer;
   void changeint(int x); //prototype, just like other prototypes
}

MyClass::changeint(int x) { //define it... :: is used to access stuff inside of the class (in this case)
   this->integer = x; //this is a pointer to the current class, used so you can have multiple classes that can use a function
// also:
// this->integer
// is the same as:
// (*this).integer
}
Very insightful, makes me think of a struct which i do know how to put together :D jus different commands to access it, such as :: to access things inside the class while in structs, the '.' is used to do that. one other thing, constructers build the class and deconstructors break it down after the class has been used?

1
2
3
4
5
6
7
8
9
class orville {
int me;
}


int main()
{
orville::me=3; //not to famaliar with pointers cus they are also confusing so jus something simple :P
}

right?
and also, if there the same as structs, when should i use a struct as oppose to a class? im still wondering what they are used for exactly, just organization? i feel like if this were to help clean up long lines of code, it wouldnt because then all the long code is just sitting in the class instead of the main function. i hope im not being to annoying and ty so much for your help you have cleared up quite a few things
[/code]
Last edited on
Well...you're close.

1
2
3
4
5
6
7
8
class orville {
   int me;
}

int main() {
   orville MyClass; //define it like a variable of type orville
   MyClass.me = 3; //use . for instances of the class
}


The :: is used for static variables (non-changing variables defined like this:
1
2
3
4
5
6
7
class orville {
   static int gravity;
}

int main() {
   orville::gravity = -10;
}
) or for defining functions (like I had above).
Last edited on
Very insightful, makes me think of a struct which i do know how to put together :D jus different commands to access it, such as :: to access things inside the class while in structs, the '.' is used to do that. one other thing, constructers build the class and deconstructors break it down after the class has been used?

'.' is still used to access "things".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class test
{
public:
void do_something(int);   //prototype of the function
}

void test::do_something(int x)   //definition of the function
{
//do stuff with x
}

int main()
{
test whatever;
whatever.do_something(5);   //accessing the function, if you will
return 0;
}

So :: can be used to define functions of a class outside of a class, which the code look better. I didnt know you could use it the way firedraco used it, but I'm still learning after all.
Guess its all how you decide to use it. and i also realized something, while structs are, from all i hear, only public. Classes you can define what you want to be protected or private which i guess is useful to the right person but i still see no real use for classes yet. when i finish actually learning all i can about C++ ill sooner or later need a class. but at least i know how to use them now. Ty everybody, i appriciate that i can actually get answered on a forum, good or bad feedback. Its better than none. Rock on!

Orville
Yes, you are correct Poke. However, constructors are not "constructors" for the class, they are constructors/destructors for the objects of the class. There is an automatic default constructor/destructor created unless you explicitly define one, then the compiler won't make one. The constructor is run when the object is created:

1
2
3
4
5
6
7
8
9
class MyClass {
   MyClass(int x);
   int MyInt;
}

int main() {
   MyClass Cons_Test(20);
   //Cons_Test.MyInt is now 20
}


The destructor is called when the object is destroyed (usually at the end of the program).
I think you've got me confused FireDraco, in the first paragraph I was simply quoting the post I was replying to. Or am I the one confused?
Ahhhh I see now XD, yeah, I was responding to the quote ^^;
Though a bit late, I just want to throw a couple of sents.

First, to explain about void, just you have quick look back at C.
C is a procedure-oriented language unlike C++.
C++ is object-oriented langauge. As it supercedes the C, it supports the c-style programming with procedures and functions etc.

The basic difference, a function returns a value and a procedure does not.

So how would you say you are writing a procedure to do something (may be a sequence of operations) but dont expect a value in return, like swap(a, b). It swaps the values of a and b vice versa but does not give back any explicit return code/value.
Where as the function pow(a,b) returns a value showing the power a^b, and can be used in of your any arithmatic expressions.

So, a chunk of code (ie, method) declared with return type 'void' is a procedure and other returns a value, is a function. In very simple terms.


And struct and class.

a structure is a group of data items declared and used in C style programming.
In addition to superceding the C and supporting the C-style programming, the C++ has its own version of object-oriented data types, such as a class supporting the encapsulization, polymorphism, inheritence, etc, etc.

In supportive to C++ style programming, the C provided 'struct' type is mould to look/act like a C++ class with simple differences like public by default (as it was in C).
The private, protected and public data properties are of object-oriented encapsulization concept and C is not built upon such concept. Hence a struct in C is just a group of data, but the same in C++, is a class-alike.

Since C++ built upon object-oriented concept and supports data inhertitence the :: (resolution) operation is provided to access those data members across the class hierarchy/family and its accessibility scope.

And finally, about construction and destruction, you have an earlier idea of same types, like
 
struct Student stu1, stu2, stu3;


In terms of C, you would say stu1, stu2 and stu3 are variables of one type, Student buti n terms C++, you would call the same as instances or objects.
Since there was no encapsulization concept in the struct type in C, you would not need to have a control of the construction and destruction of those struct variables.
But in C++, you may want to control the construction and destruction of a class you define what, when, and how. So the methods are provided for your control.
(When you dont provide one, the compiler would provide you one, a basic constructor and destructor.
You would know when you need one, an explicit constructor or destructor, to be defined. For instance the class has a pointer and it needs to be allocated every time an object of the class is created, and deallocated/released properly when the object is destroyed.
The same for copy constructor, = operator etc.)

For more details, you could look in a nice C++ reference for objects, constructors, destructors, etc. You would have a better and clear understanding of it.

Hope this helps. Good luck :)











Topic archived. No new replies allowed.