12 Replies - 731 Views - Last Post: 06 September 2011 - 03:48 AM Rate Topic: -----

#1 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Classes Tutorial

Posted 05 September 2011 - 07:00 AM

I was wondering if anyone new of a good classes tutorial, all the ones i found didn't explain them well enough, i am looking for a good in depth one that explains it step by step,
thanks in advance
Is This A Good Question/Topic? 1
  • +

Replies To: Classes Tutorial

#2 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Classes Tutorial

Posted 05 September 2011 - 07:38 AM

I also sucked at classes and grieved about it for about a full day before I created my own reference notes on them by a compilation of code examples, explanations and concepts. I used CPlusPlus.com, LearnCpp.com and DreamInCode.net B)

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. :) You're not the only one who is/was weak in classes at first. :D

This post has been edited by hulla: 05 September 2011 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,405
  • Joined: 25-December 09

Re: Classes Tutorial

Posted 05 September 2011 - 07:40 AM

Have you looked at the two Class links from this page: C++ Language Tutorial. If you did what don't you understand?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Re: Classes Tutorial

Posted 05 September 2011 - 08:49 AM

Anyone know of a good structs tutorial, any help will be appreciated
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Classes Tutorial

Posted 05 September 2011 - 08:52 AM

Have you checked out the DIC Tutorials yet?
Was This Post Helpful? 1
  • +
  • -

#6 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • Posts: 1,688
  • Joined: 13-March 10

Re: Classes Tutorial

Posted 05 September 2011 - 08:56 AM

I will give you 2 min tutorial.

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.
Was This Post Helpful? 3
  • +
  • -

#7 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Re: Classes Tutorial

Posted 05 September 2011 - 08:57 AM

thanks it makes more sense now
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Classes Tutorial

Posted 05 September 2011 - 08:59 AM

I've gone ahead and merged your two threads requesting tutorials since the answers in your first one are relevant for both topics. :)
Was This Post Helpful? 1
  • +
  • -

#9 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Re: Classes Tutorial

Posted 05 September 2011 - 09:00 AM

thanks, at least there are still cool people in this world
Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Classes Tutorial

Posted 05 September 2011 - 10:39 AM

View PostHadean Fall, on 05 September 2011 - 04:49 PM, said:

Anyone know of a good structs tutorial, any help will be appreciated

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.
Was This Post Helpful? 0
  • +
  • -

#11 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Classes Tutorial

Posted 05 September 2011 - 10:39 PM

You forgot about the fact that a class can contain functions. Structs cannot contain functions.
Was This Post Helpful? -2
  • +
  • -

#12 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Classes Tutorial

Posted 06 September 2011 - 03:47 AM

Yes they can. They cannot in C (where classes do not exist), but they can in C++.

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

Was This Post Helpful? 1
  • +
  • -

#13 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Classes Tutorial

Posted 06 September 2011 - 03:48 AM

Oh sorry. My mistake. :s
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1