Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, i've just started learning c++ as i know java and like the way OOP works and i like the C language therefore i thought it was a good ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2004
    Location
    Birmingham
    Posts
    70

    C++ OOP logic help


    Hi all,

    i've just started learning c++ as i know java and like the way OOP works and i like the C language therefore i thought it was a good idea to learn C++. But the way OOP works in java and C++ seems quite different and my java knowledge seems to be objecting when it comes to learning C++, let me explain.

    Lets say i have 2 classes. One called Test and the other is called MyVal.

    The class MyVal has one attribute x and 2 functions, one called setX which takes an integer as a parameter and changes the value of x to the value of the parameter. The other function is called getX which simply returns the current value of x.

    The class called Test simply instantiates MyVal, changed its x value and displays the current x value on the monitor.

    Now if i was programming this under java id simply create 2 classes, which each will have its seperate .java file and as long as these classes are in the same project the two classes can instantiate each other, even though they arent in the same .java file.

    This doesnt seem to be the case with C++. As far as i can figure you wither need to put all your classes in the same .cpp file so you instantiate them or you need to create all your classes as header files and your one main class as the cpp file and you main class then imports all the header files. So for this example the myVal class would be a header file and the test class would be in the .cpp file as it is the main class and therfore it imports MyVal.h.

    You can probably tell im slightly confused with the logic of OOP behind c++ as it isnt the same as java (because what i can tell two .cpp files cannot communictae with each other even if they are in the same project or workspace).

    So is the correct way to program OOP in c++ by creating all the class files you intend to use as header files and the main class as a .cpp file and the main class imports them. Or do you just include all the classes you wish to use in the same .cpp file?

    Thanks for any help, and sorry for the long, stupid question, i just cant seem to get my head around this.

  2. #2
    Just Joined!
    Join Date
    Feb 2005
    Location
    Ottawa, Canada
    Posts
    39
    You will have your main.cpp and main.h
    and whatever other classes you want to include (class1.cpp, class1.h -- class2.cpp, class2.h -- etc.)

    You have to have the proper templates in the .h ect.

    Then simply include them in your main class and bob's your uncle


    Sorry for the french coding, don't have time to translate it =)
    Feel free to ask any questions

    Code:
    // Lab_02.cpp
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    #include <conio.h>
    
    #define PI 3.1415926
    
    #include "Contenant.h"
    #include "Boite.h"
    #include "Sphere.h"
    #include "Sphere.cpp"
    
    using namespace std;
    
    
    // Programme principal
    
    int main&#40;int argc, char* argv&#91;&#93;&#41;
    &#123;
    	Contenant<float> *cont;
    	char reponse;
    
    	cout << "Boite ou Sphere &#40;b/s&#41;? ";
    	cin >> reponse;
    
    	switch &#40;reponse&#41; &#123;
    	case 'b'&#58;
    	case 'B'&#58; cont = new Boite<float>; 
    			  break;
    	case 's'&#58;
    	case 'S'&#58; cont = new Sphere<float>;
    			  break;
    	default&#58; return 1;
    	&#125;
    
    	cont->demanderDonnees&#40;&#41;;
    
    	cout << "Surface = " << cont->surface&#40;&#41; << endl;
    	cout << "Volume  = " << cont->capacite&#40;&#41; << endl;
    
    	cout << "Pressez une touche pour terminer...";
    	cin.get&#40;&#41;;
    	cin.get&#40;&#41;;
    //	_getch&#40;&#41;;
    
    	return 0;
    &#125;
    Code:
    //sphere.cpp
    #include <iostream>
    #include "Sphere.h"
    
    using namespace std;
    
    //const double PI = 3.141592654;
    
    
    // Constructeur par défaut
    template <class T>
    	Sphere<T> &#58;&#58; Sphere<T>&#40;T ra&#41; &#123;
    		rayon = ra;
    	&#125;
    
    // Constructeur par copie
    template <class T>
    	Sphere<T> &#58;&#58; Sphere<T>&#40;const Sphere<T> &copie&#41; &#123;
    		rayon = copie.rayon;
    	&#125;
    
    // Surcharge&#58; calcule le volume de la boite.
    template <class T>
    	T Sphere<T> &#58;&#58; capacite&#40;&#41; &#123;
    		return &#40;PI*rayon*rayon&#41;;
    	&#125;
    
    // Surcharge&#58; additionne l'aire des 6 surfaces
    //            de la boîte.
    template <class T>
    	T Sphere<T> &#58;&#58; surface&#40;&#41; &#123;
    		return &#40;2 * PI * rayon&#41;;
    	&#125;
    
    // Surcharge de l'interface textuel permettant 
    // d'initialiser l'instance selon les données
    // fournies par l'utilisateur
    template <class T>
    	void Sphere<T> &#58;&#58; demanderDonnees&#40;&#41; &#123;
    		cout << "Rayon? ";
    		cin >> rayon;
    	&#125;
    Code:
    #ifndef SPHERE_H
    #define SPHERE_H
    
    #include "Contenant.h"
    
    
    // Classe représentant un contenant sphérique
    template <class T>	
    	class Sphere &#58; public Contenant<T> &#123;
    	public&#58;
    	// Constructeur par défaut
    	Sphere<T>&#40;T = 0&#41;;
    
    	// Constructeur par copie
    		Sphere<T>&#40;const Sphere<T> &&#41;;
    
    	// Surcharge&#58; calcule le volume de la boite.
    		T capacite&#40;&#41;;
    
    	// Surcharge&#58; additionne l'aire des 6 surfaces
    	//            de la boîte.
    		T surface&#40;&#41;;
    
    	// Surcharge de l'interface textuel permettant 
    	// d'initialiser l'instance selon les données
    	// fournies par l'utilisateur
    	void demanderDonnees&#40;&#41;;
    
    	private&#58;
    		T rayon;
    	&#125;;
    
    #endif
    Code:
    #ifndef CONTENANT_H
    #define CONTENANT_H
    
    // Classe mère abstraite permettant une utilisation
    // polymorphique des classes dérivées
    template <class T>
    	class Contenant &#123;
    	public&#58;
    		virtual T capacite&#40;&#41; = 0;
    		virtual T surface&#40;&#41; = 0;
    
    		virtual void demanderDonnees&#40;&#41; = 0;
    	&#125;;
    
    #endif
    Code:
    #ifndef BOITE_H
    #define BOITE_H
    
    #include "Contenant.h"
    
    
    // Classe représentant un contenant rectangulaire
    template <class T>
    	class Boite &#58; public Contenant<T> &#123;
    	public&#58;
    		// Constructeur par défaut
    		Boite<T>&#40;T lo=0, T la=0, T ha=0&#41; &#123;
    			longueur = lo;
    			largeur  = la;
    			hauteur  = ha;
    		&#125;
    
    		// Constructeur par copie
    		Boite<T>&#40;const Boite<T> &copie&#41; &#123;
    			longueur = copie.longueur;
    			largeur  = copie.largeur;
    			hauteur  = copie.hauteur;
    		&#125;
    
    		// Surcharge&#58; calcule le volume de la boite.
    		T capacite&#40;&#41; &#123;
    			return &#40;longueur * largeur * hauteur&#41;;
    		&#125;
    
    		// Surcharge&#58; additionne l'aire des 6 surfaces
    		//            de la boîte.
    		T surface&#40;&#41; &#123;
    			return &#40;2 * longueur * largeur +
    					2 * longueur * hauteur +
    					2 * largeur  * hauteur&#41;;
    		&#125;
    
    		// Surcharge de l'interface textuel permettant 
    		// d'initialiser l'instance selon les données
    		// fournies par l'utilisateur
    		void demanderDonnees&#40;&#41; &#123;
    			cout << "Longueur? ";
    			cin >> longueur;
    
    			cout << "Largeur?  ";
    			cin >> largeur;
    			cout << "Hauteur?  ";
    			cin >> hauteur;
    		&#125;
    
    	private&#58;
    		T longueur, largeur, hauteur;
    	&#125;;
    
    #endif

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Location
    Ottawa, Canada
    Posts
    39
    And and you even get a preview of templates with that

  4. #4
    Just Joined!
    Join Date
    Sep 2004
    Location
    Birmingham
    Posts
    70
    Thankyou so much for the detailed reply i believe i understand it now. It will probably all go pair-shaped when i get home to try it lol. Thanks again

  5. #5
    Just Joined!
    Join Date
    Feb 2005
    Location
    Ottawa, Canada
    Posts
    39
    Just to add a bit more... Ideally, you would always do it the way sphere.h and sphere.cpp are done.

    If you make an application for someone, you don't want them to see the code, just the functions that they can use (the .h) and you would hide the .cpp from them.

    Putting the function's code in the .h is not right (this was a school project and they just wanted us to realize that it didn't matter where you put the code)

    But the right way to do it is to have a .h and .cpp with the .h only being the function headers and parameters.

    ----------------------

    I don't know if you know about polymorphism but this is a perfect example... I make a pointer of a 'contenant' (container) and I can make it point to a 'sphere' or a 'boite' because contenant's functions are virtual

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •