Jump to content
  • Advertisement
Sign in to follow this  
wabbz111

c++ friend classes

This topic is 1323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having issues trying to use friend classes. I want the class Porsche to use method Steer() found in vehicle class but i have tried several styles without success could someone point me in the right direction.
With the class Lamborghini i used inheritance and it works fine but i wanted to use friend classes for the Porsche class. My code is below
 Thanks
 




<code>
#include <cstdlib>
#include <iostream>

using namespace std;



class Vehicle
{

public:
    friend class Porsche; //porsche as friend to vehicle
    void Steer()
    {
      vLeft = "steering left";
      vRight = "steering right";
       
      
     // if (vLeft)
		cout <<vLeft<< endl;
	  //if(steering Right)
		cout << vRight << endl;  
        //cout
    }
   int Accelerate()
    {
        vAcceleration = 5.0f;
        cout<<"acceleration takes "<<vAcceleration<<" seconds "<<endl;
        return vAcceleration;
    }
    int Brake()
    {
       
        cout<<"Brakes applied"<< vBrake<<endl;
        
    }
private:
    float vAcceleration;
    string vBrake;
    int vTopSpeed;
    string vLeft;
    string vRight;
     //declare porsche as friend to vehicle
    
};

class Porsche
{
    
    public:
     friend class Vehicle; //declare vehicle as friend to porsche
     string pName;
     int PorscheAccelerate()
        {
            cout<<" Porsche accelerates in "<<pAcceleration<<" seconds "<<endl;
        }
     int PorscheTopSpeed()
        {
            pTopSpeed = 350;
            cout<<" Porsche Cayenne has a top speed of "<<pTopSpeed<<" KM/H "<<endl;
        }
    int PorscheHorsePower()
        {
            pHorsePower=460;
            cout<<" Porsche produces "<<pHorsePower<<endl;
        }
  //* 
   void PorscheSteer(Vehicle porsche)
    {
       // Steer();
    }//*/
    private:
        float pAcceleration;
        string pBrake;
        int pTopSpeed;
        int pHorsePower;
       
    
};

class Lamborghini : public Vehicle
{
    public:
     string LamboName;
     int LamboAccelerate()
        {
            cout<<" Lamborghini accelerates in "<<lAcceleration<<" seconds "<<endl;
        }
     int LamboTopSpeed()
        {
            lTopSpeed = 440;
            cout<<" Lamborghini has a top speed of "<<lTopSpeed<<" KM/H "<<endl;
        }
    int LamboHorsePower()
        {
            lHorsePower=1000;
            cout<<" Lamborghini produces "<<lHorsePower<<" HP"<<endl;
        }
    private:
        float lAcceleration;
        string lBrake;
        int lTopSpeed;
        int lHorsePower;
    
};
int main(int argc, char *argv[])
{
    //objects for vehicle class
    cout<<" _________________objects for vehicle________________________"<<endl;
    Vehicle car;
    car.Steer();
    car.Accelerate();
    //cout<<car.vRight;
    cout<<" _________________objects for porsche________________________"<<endl;
    cout<<endl;
    Porsche Cayenne;
    cout<<Cayenne.pName;
    Cayenne.PorscheTopSpeed();
    Cayenne.PorscheHorsePower();
   // Cayenne.Steer(car);
    cout<<endl;
    cout<<endl;
    
    cout<<" _________________objects for lamborgini________________________"<<endl;
    cout<<endl;
    Lamborghini Murcielago;
    Murcielago.LamboHorsePower();
    Murcielago.Steer(); // using steer function found only in vehicle class
    cout<<endl;
    //car.vBrake = "Braking"; //use this as private
    //cout << "Press the enter key to continue ...";
    //cin.get();
    system("pause");
    return EXIT_SUCCESS;
}
</code>

Share this post


Link to post
Share on other sites
Advertisement

Thanks @Paradigm Shifter i will now try it with the Steer() method private. I added porsche.Steer(); in PorscheSteer(); then in the main method called Cayenne.Steer(car); and it worked.

 

@SeanMiddleditch  I hear you great advice but if you dont mind how about an example to clear up any lingering concerns.Regards

Share this post


Link to post
Share on other sites
From my experience friend classes are used to access members of objects that makes sense to explicitly expose to the class that uses it. That's a general definition.

A Porshe is a vehicle and can access protected members of its parent. So doesn't makes sense using friend classes in this simple problem.

A veichle has everything related to cars. They have 4 wheels and they can't fly (as well some other characteristics).

Share this post


Link to post
Share on other sites

@SeanMiddleditch  I hear you great advice but if you dont mind how about an example to clear up any lingering concerns.Regards


A quick partial example, since I have work to do and all. smile.png
 
// just this one class, no friends or children needed to model different cars
class Vehicle {
  string _name;

  // these are common to all vehicles; no need to recode it for each different car manufacturer!
  // different cars differ in the data that describes them, not in basic operation.
  // even for things like multiple gears and transmissions, you can model that with more data
  // describing the difference between car models rather than hard-coding the logic in C++.
  float _acceleration = 0;
  float _brakePower = 0;
  float _topSpeed = 0;

public:
  /* constructor, setters, getters, etc. */

  void Steer(float direction) { /*...*/ }
}

int main() {
  vector<Vehicle> vehicles;

  // no hard-coding the vehicles; they're all defined in some text file with whatever format you like
  // an unstructured list of values is usually easiest to learn how to read with IOStreams, though
  // you'll want to move on to a more readable/manageable format as soon as you're comfortable doing so
  vehicles = LoadAllVehiclesFromTextFile("my_vehicles.txt");

  // we now just have a collection of vehicles; could 0, 1, or 200 different vehicles
  // depending on what you put in your text file
  for (auto& vehicle : vehicles) {
    cout << vehicle.GetName() << '\n';

    // you are programming against the abstraction called Vehicle and not to a specific car manufacturer;
    // this is what good (albeit simplified) Object-Oriented Programming is all about (notice that no inheritance was needed at all!)
    vehicle.Steer(/*some direction*/);
    cout << "Speed:" << vehicle.GetSpeed() << " Heading:" << vehicle.GetHeading() << '\n';
  }
}

Share this post


Link to post
Share on other sites

@SeanMiddleditch I will definetly try this one out. Meanwhile let me do more practice i will also be posting another program(using classes vehicle and bicycle) soon that hopefully can use friend classes beter.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!