Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » Keyword: this (pointer)
      by Valmont
 Page 2 of 3 
< Previous     Next >

(Login to remove green text ads)
The Problem
The Problem


Straight into code:
Code:
//main.cpp

#include <iostream>
#include <string>

using namespace std; //Ok for this very small project.

class CSomeClass
{
public:
  void Print(string text)
  {
    cout<<text<<endl<<endl;
  }

};


int main(int argc, char* argv[])
{
  CSomeClass obj_1;

  obj_1.Print ("Object 1 made me print this");

  return 0;
}
Just about the most simple working code with a class and some feedback.
The code is not getting more complex than this, but we need to add just one more instance of class CSomeClass:
Code:
#include <iostream>
#include <string>

using namespace std; //Ok for this small project.

class CSomeClass
{
public:
  void Print(string text)
  {
    cout<<text<<endl<<endl;
  }
};


int main()
{
  CSomeClass obj_1;
  CSomeClass obj_2;

  obj_1.Print ("Object 1 made me print this");
  obj_2.Print ("Object 2 made me print this");

  return 0;
}
Ever wondered how CSomeClass::Print() knew which text to print correctly?
One could say: 'Well, you've typed it. It's right there on paper.'
But that wasn't the question.
How did void CSomeClass::Print() knew?
Knowing the answer to my question is the whole point of this tutorial.
Remember, there is only one unique void CSomeClass::Print(char* text). Just look at the class above.
See the image below for a demonstration of this problem:


Before an answer is given, a small recap on objects will be given first.




 
 Page 2 of 3 
< Previous     Next >

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting