Classes Tutorial
Page 1 of 112 Replies - 626 Views - Last Post: 06 September 2011 - 03:48 AM
#1
Classes Tutorial
Posted 05 September 2011 - 07:00 AM
thanks in advance
Replies To: Classes Tutorial
#2
Re: Classes Tutorial
Posted 05 September 2011 - 07:38 AM
I suggest you try the same approach.
Classes are actually very easy for me, it's just that I was never very good at data-structures so I suggest, if you are bad at structs then practice, practice, practice them until you become very good.
All the best.
This post has been edited by hulla: 05 September 2011 - 07:50 AM
#3
Re: Classes Tutorial
Posted 05 September 2011 - 07:40 AM
Jim
#4
Re: Classes Tutorial
Posted 05 September 2011 - 08:49 AM
#5
Re: Classes Tutorial
Posted 05 September 2011 - 08:52 AM
#6
Re: Classes Tutorial
Posted 05 September 2011 - 08:56 AM
Unlike arrays, structs can contain different types, e.g.
struct person
{
string name;
int age;
}; //note the semi colon!!!
You can initialize them like arrays!:
person a = {"bob the builder",12};
You can get the stuff out!:
void printPerson(person p)
{
std::cout << p.name << std::endl;
std::cout << p.age << std::endl;
}
If you pass the pointer to the struct, you CANT use dot operator!:
void printPerson2(person* p)
{
std::cout << p->name << std::endl;
std::cout << p->age << std::endl;
}
You could use the dot operator in the above example but then you would have to do:
void printPerson3(person* p)
{
std::cout << (*p).name << std::endl;
std::cout << (*p).age << std::endl;
}
Good luck.
#7
Re: Classes Tutorial
Posted 05 September 2011 - 08:57 AM
#8
Re: Classes Tutorial
Posted 05 September 2011 - 08:59 AM
#9
Re: Classes Tutorial
Posted 05 September 2011 - 09:00 AM
#10
Re: Classes Tutorial
Posted 05 September 2011 - 10:39 AM
Hadean Fall, on 05 September 2011 - 04:49 PM, said:
This is essentially a repeat of your original question - structs and classes are identical in C++. The only difference is their default access-specifier for members (If you don't know what that is yet, then you will after reading one of the tutorials on classes). In other words, there's absolutely nothing which a class can do which cannot also be done in exactly the same way using a struct.
#11
Re: Classes Tutorial
Posted 05 September 2011 - 10:39 PM
#12
Re: Classes Tutorial
Posted 06 September 2011 - 03:47 AM
You're thinking of C#.
In this language, structs are very different to classes (though they exist side by side). In C#, all classes are created on the heap (think Foo foo = new Foo();) whereas structs are created on the stack, and also cannot contain functions (think Foo foo;[il]).
However, in C++ [il]struct Foo { is exactly identical in all ways to:
class Foo {
public:
Note that is considered bad style to use structs for objects designed to have many functions. A struct is usually preferred if you have no or very few functions. Constructors and operators are also often included in structs.
This post has been edited by PlasticineGuy: 06 September 2011 - 03:48 AM
|
|

New Topic/Question
Reply



MultiQuote








|