Objectify Me

I'm trying to orient myself to C++ comming from C#;

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1219 Views - Last Post: 22 December 2009 - 07:31 PM Rate Topic: -----

#1 iCoCoA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 09

Objectify Me

Posted 22 December 2009 - 11:58 AM

Hi folks, I'm having a simple issue understanding "objects" and classes in C++. I'm trying to learn C++ after doing C# for about a year and I can't understand how to instantiate objects from other files I've created in C++. For example:

#include <iostream>

using namespace std;

int main()
{
	anotherTest test;

	test.DisplayText();

	cout << "First line of text From main." << endl;

	return 0;
};

class anotherTest
{
public:

	void anotherTest::DisplayText()
	{
		cout << "Second line of text from another class." << endl;
	}
}


This code works fine. But when I place the second class "anotherTest" into another file (I.E. remove it from classA.cpp to classB.cpp) like this:

ClassA.cpp


#include <iostream>

using namespace std;

int main()
{
	anotherTest test = new anotherTest();

	test.DisplayText();

	cout << "Yep I'm done!" << endl;

	return 0;
}


ClassB.cpp

#include <iostream>

using namespace std;

class anotherTest
{
public:

	void anotherTest::DisplayText()
	{
		cout << "Hello Folks Again!" << endl;
	}
};


I cannot get it to work correctly. I have no knowledge on how to create classes across multiple files. Maybe someone can enlighten me on how to do this and to get it working properly. I have been reading multiple books at Barnes and Noble trying to find a solution to my problem and also searching online. Maybe I'm just overthinking something? Or maybe I'm lacking a certain concept? I'm not sure. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Objectify Me

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Objectify Me

Posted 22 December 2009 - 12:09 PM

You need to include your header in your main() source file. Change class B to a header file and add the name of the header and It should work. Example

MAIN.CPP
#include <iostream>
#include "SOME_HEADER_FILE.h"

int main()
{
	anotherTest test;

	cout<<test.DisplayText()<<endl;

	cout << "Yep I'm done!" << endl;

	return 0;
}



SOME_HEADER_FILE.h
#include <string>
using namespace std;

class anotherTest
{
public:

	string DisplayText()
	{
		return "Hello Folks Again!";
	}
};



Its usually better not to use output streams within your classes such as 'cout' because it leaves your class console dependent. You should return the string from you function then display it in main

This post has been edited by ImaSexy: 22 December 2009 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 iCoCoA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 09

Re: Objectify Me

Posted 22 December 2009 - 12:18 PM

Basically in C++ I can create classes in header files and include them in my main file (.cpp file) to use their functions and also instantiate them? How are header files any different than .cpp files in terms of object oriented programming?
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Objectify Me

Posted 22 December 2009 - 12:33 PM

you use cpp files to define your functions with in your header file

for your project, heres an example

MAIN.cpp
#include <iostream>
#include "SOME_FILE.h"

int main()
{
	anotherTest test;

	cout<<test.DisplayText()<<endl;

	cout << "Yep I'm done!" << endl;

	return 0;
}



SOME_FILE.h
#include <string>
using namespace std;

class anotherTest
{
public:

	string DisplayText();
  
};



SOME_FILE.cpp
#include SOME_FILE.h

 string anotherTest::DisplayText()
	{
		return "Hello Folks Again!";
	}


This post has been edited by ImaSexy: 22 December 2009 - 12:42 PM

Was This Post Helpful? 1
  • +
  • -

#5 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Objectify Me

Posted 22 December 2009 - 12:46 PM

I always thought of the Header(.h) files as the connecting pieces or bridges to the Source(.cpp) files. I store the information regarding the program in Source files and a bridge that data into my main using Header files. Much like Ima said, Header files are used fore defining functions, variables and any other 'object' which may need to be global.

EDIT: you need quotes on the second include Ima

This post has been edited by GenHornet18: 22 December 2009 - 12:48 PM

Was This Post Helpful? 1
  • +
  • -

#6 iCoCoA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 09

Re: Objectify Me

Posted 22 December 2009 - 12:57 PM

So just to make sure that I understand correctly, Main is used as some sort of "manager" function. And in order to access other files functions you use "header" files to "describe" them to main?
Was This Post Helpful? 0
  • +
  • -

#7 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Objectify Me

Posted 22 December 2009 - 01:10 PM

Ya pretty much, the header files tell main what is being called. It all makes sense when you think that all of this is compiled into a larger program. The main function and therefore file is executed first and from there any other functions, variables etc. that are called to from main but reside outside the main file need to be connected or recognized, and so the header file which is included in both the main and other file does this by connecting the two files.

main.cpp + header.h + file2.cpp
Was This Post Helpful? 1
  • +
  • -

#8 iCoCoA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-December 09

Re: Objectify Me

Posted 22 December 2009 - 01:24 PM

Thanks for the awesome reply, that was a real helpful explanation. If you don't mind I have a few more questions about the C++ Language.

#1. How do I define "constructors" in C++?

What I know: classname::classname(); But the question is, do I have to "prototype" it in the header file also? Or only in the class (.cpp) file? And I can do any initializations inside the constructor that I want to do just like in other languages?

#2. For each "source" (.cpp) file I have there has to be a header created for it?

What I know: You prototype the methods in the header file (.h file) and do all the "work" in the source file (.cpp file). Does this only involve when using methods (functions)? Or do I have to "prototype" variables also?

#3. When I create a header file for a .cpp file, does the actual "physical" layout of the .h (header) file have to match the layout of the .cpp file?

(I.E.)
class object
{
public: 

	int displayText(int num1, int num2);

	int displayMoreText(int num1, int num2, int num3);
};


 #include "object.h"

int object::displayText(int num1, int num2)
{
	int sum;

	sum = num1 + num2;

	return sum;
}

int object::displayMoreText(int num1, int num2, int num3)
{
	// do stuff here;
}


#4. And from what I can see, header files are just "descriptions" of what "functions" lie ahead. So can I do any "logical, arithmetic, or conditional" work inside of a .h header file? (I.E. "If conditionA == true" then "do this function?") Or should all that be done inside of main?

Thanks in advance :)

This post has been edited by iCoCoA: 22 December 2009 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Objectify Me

Posted 22 December 2009 - 02:12 PM

just rememver main() is the entry point of your program.

#1 example (there is a default construtor also)
 
class example
{
private:
	int x;
	int y;
public:
	example()
	{
		x = 10;
		y = 20; // dummy values
	}
};



to define your variables dynamically just add parameters to your constructor


#2 You can define functions in your header, Its just more readable to define them in another source file *cpp

#3 What do you mean Layout?? Its just used to define whatevers in your . h file
#4 you can do whatever you like in your header file
Was This Post Helpful? 0
  • +
  • -

#10 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Objectify Me

Posted 22 December 2009 - 02:30 PM

When I jumped from C# to C++ I had the same problem i.e.. .h and .cpp
as because in C# we all use a singal file .cs so whats all about ,it is the same just in C# you never looked at the .h file that is the metadata file if you rem.. then when we debug our code in C# and used any method(i.e.. in c++ called a function) from a reference like "System" you would see the metadata,not the function code try it ,Now comes the C++ why to use its native faster than C# i.e.. CLR ,.The header files just keeps track of what functions,data type are their in its .cpp file i..e.. how the function looks like means what parameters are their and whats its return type it is their because our compiler checks the use of the functions with the proto type and if they are used properly then no error else error so the header is included without the .h also you can do the stuff for example
//One.cpp
int add(int a,int b)
{
 return a+b;
}


Now our main file
#include <iostream>
using namespace std;
int add(int a,int b); //I decl.. the prototype here
int main()
{
cout<<add(10,10);
return 0;
}



So main stuff is that the compiler should know what the function looks like just that.

Now your ans1
class Myclass
{
public:
		Myclass() //constructors
		{
		}
		 ~Myclass() //destructor
		 {
		 }
};



#2. For each "source" (.cpp) file I have there has to be a header created for it?

Yes,you declare your variables in the header only

#3. When I create a header file for a .cpp file, does the actual "physical" layout of the .h (header) file have to match the layout of the .cpp file?

Yes but it can be like this also
class object
{
public: 

	int displayText(int, int );

	int displayMoreText(int , int , int );
};



And for the last yes you can do it inside the header as i have done it above with the constructor and that is called inline for long code inside a function this is not best but for small code it do well.

Good Luck

This post has been edited by debjit625: 22 December 2009 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Objectify Me

Posted 22 December 2009 - 04:24 PM

The information provided by ImaSexy and debjit is at best misleading and worst just factually wrong. Here are my responses to some of their statements:

ImaSexy said:

You can define functions in your header, Its just more readable to define them in another source file *cpp
It's not a question of readability. C++ has a one definition rule. Header files are included in many source files. So if you have a definition in a header file, which then gets included in multiple source files, multiple definitions are created. On linking, this creates a multiple definition error from the linker.

debjit said:

The header files just keeps track of what functions,data type are their in its .cpp file
The header file doesn't "track". The header file is a convention. It's motivated because declarations need to be repeated across source files. So instead of doing it manually, we dump everything in one single file (called by convention a header file) and copy paste using the #include preprocessor directive. The header file does not maintain, synchronize, or do anything else.

Quote

because our compiler checks the use of the functions with the proto type and if they are used properly then no error else error
The compiler does not do this checking. In fact, this lack of checking causes problems in C (not C++) where the compiler can validly assume a function signature. This leads to funny bugs when doing something like calling malloc without including the appropriate header. But as I noted, there is no comparison of declarations and definitions.

debjit said:

#2. For each "source" (.cpp) file I have there has to be a header created for it?

Yes,you declare your variables in the header only
Misleading sentence. A source file does not require a matching header file. You can have 0, 1, or five million header files. It's up to you.

Quote

#3. When I create a header file for a .cpp file, does the actual "physical" layout of the .h (header) file have to match the layout of the .cpp file?

Yes but it can be like this also
Misleading statement. There is no enforced layout.

iCocoa said:

What I know: classname::classname(); But the question is, do I have to "prototype" it in the header file also? Or only in the class (.cpp) file?
You have two options. You can declare its existence in the class definition. Then define the constructor in some source file. Or you can have an inline definition within the class definition.

Quote

Does this only involve when using methods (functions)? Or do I have to "prototype" variables also?
Absolutely true for variables. You don't frequently do so for variables because you don't use global variables except in truly necessary circumstances. But, when you have global variable in one source file, that is referenced in other source files, you must declare its existence. You can manually type out the declaration in every file that needs it, or put the declaration in a header file and include the header file.
Was This Post Helpful? 1

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Objectify Me

Posted 22 December 2009 - 05:01 PM

Some of the confusion here is that the same words are being used in different ways in different posts.

In my mental map I use these two words "definition" and "implementation" to be different things with clear meanings.
(Not that my version is right and others are wrong but at least one explicit definition of terms might help make some of the above make more sense.)

In the most common layout (as Oler1s points out these are not requirements, just conventions) we divide what goes in headers (.h) and what goes in implementation (.cpp) files.

The "definitions" of a class belong in the header (.h) file.
Some (inline) implementations also appear in the header.

The bulk of implementations go in the implementations (.cpp) file

So an example might be:
In a .h file a class method is defined like this
int theMethod();
Therefore there needs to somewhere be an implementation of that class method.
If it is not a trivial method then convention is to put this in a separate implementation file.
int theClass::theMethod() {
   // do some stuff here
   return result;
}



Does that make sense what I mean by "definition" and "implementation"?

So if you go back through the above you will see different usages of those terms (as I say these are valid just different) and with this model can you see what is intended by the different uses in different places?

For example when ImaSexy says:
"You can define functions in your header, Its just more readable to define them in another source file *cpp"
I would write it like this:
"In the most common layout you define class methods in your header. You can also place the implementation of a method in the header file but there are a range of reasons why it is a good idea to separate definition and implementation and so we often place the implementation of a method in a separate implementation file. There are exceptions to this rule that will become clearer in time."

Where ImaSexy uses "define" I use "implement".
Where ImaSexy uses "function" I use "method" (because I believe it is a good idea to have a different name for what happens in a class than what happens in a *normal* function).

Anyway I hope that this helps you interpret what has been said in this thread a little more clearly.

The problem of not having rigidly defined terminology is a constant problem in development and a source of much confusion to beginners (and others).

You need to be aware of the different naming schemes out there and try to translate them as you encounter them. If you really aren't sure what the author means in their use of a term be wary of guessing. Keep any guesses flagged in your mind with a question mark and look for evidence that might help make it clearer what the author means and review any assumptions as more evidence comes to light.

Some more reading that might help
http://www.java2s.co...lementation.htm
http://www.octopull....ion_hiding.html
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Objectify Me

Posted 22 December 2009 - 05:13 PM

sorry for the confustion. It was a rushed post
Was This Post Helpful? 0
  • +
  • -

#14 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Objectify Me

Posted 22 December 2009 - 05:19 PM

This is not a fight I think so ,I have some questions to Oler1s
1)You said

Quote

The header file doesn't "track". The header file is a convention. It's motivated because declarations need to be repeated across source files.


Why do we need to do declarations in C or C++.And does this statement goes with this one you made

Quote

It's not a question of readability. C++ has a one definition rule. Header files are included in many source files. So if you have a definition in a header file, which then gets included in multiple source files, multiple definitions are created. On linking, this creates a multiple definition error from the linker.


Quote

The compiler does not do this checking. In fact, this lack of checking causes problems in C (not C++) where the compiler can validly assume a function signature. This leads to funny bugs when doing something like calling malloc without including the appropriate header. But as I noted, there is no comparison of declarations and definitions.


Yes compilers do check in case of C its less and in C++ its more and its called type checking, when you use a "string"(char array) in place of int the compiler gives error ,and that why we do declaration so that the compiler could understand what type of data is needed at that place

Quote

there is no comparison of declarations and definitions


?????? so why to use or say about them , offcourse their is a big difference
When we declare a function we say that their is a function which returns a type of data and take a kind of data as parameters.
Now comes definition, when we write the whole function body thats the code for the function we say its the definition.
Just in case of simply english these word means like this only isnt?

Here is a declaration which says we have a function named "fun" it returns integer and as parameters it takes an integer for the first parameter and an integer for the second parameter.
int fun(int,int);



Now the definition what should it do i.e.. program logic
int fun(int a,int b)
{
return a+b;
}


it just add two integer got from its parameters

for variables(our data members) its definition is its declaration but it could be seperated also using "extern" keyword

Quote

Misleading sentence. A source file does not require a matching header file. You can have 0, 1, or five million header files. It's up to you.

Read the total stuff what iCoCoA have writen....

Quote

Misleading statement. There is no enforced layout

iCoCoA asked for "physical" layout , if their is no "physical" enforced layout show us one.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Objectify Me

Posted 22 December 2009 - 05:38 PM

View PostImaSexy, on 22 Dec, 2009 - 04:13 PM, said:

sorry for the confustion. It was a rushed post


Sorry for picking on your posting ImaSexy
It wasn't because it was *bad* but simply because it was short.
I just wanted to explore the various terms in use out there and your post was a nice short example to work against.
As I say, my terminology isn't right and yours wrong.

(BTW I don't think that your use of "readability" to encompass all the issues around the goodness of separating definition from implementation was awful. But I thought I'd better go with the longer form so it didn't look like we were disagreeing with Oler1s's expansion. You agree that Oler1s's wording is a little more clear than your shorthand don't you? I think we are all on the same side on that point. Yours is good, Oler1s's is better.)
Was This Post Helpful? 1

  • (2 Pages)
  • +
  • 1
  • 2