Hello everyone, I have got some questions and i am really hopping someone could help me out with them. I have a class privately encapsulated in another class. Now, I'm trying to make an object of it in my testDriver but I'm not quite sure how to do that. I've tried a bunch of things but it resulted in failure.

#include <vcl.h>
#pragma hdrstop
#include<iostream>
#include"Flights.h"
using namespace std;

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[]){
	 string Adl, Mel;
   //	FlightsTL::Flight::FlightsTL:Flight::Flight b ;


	cout<<"check if this works and then we are in business\n";

	system("pause");
	return 0;
}

I've written out all the code declarations in my header file and I've implemented it in the following CPP file (I'm going to put the code down). This is what my code looks like:

#include"Flights.h"

          //Start House-keeping functions
		  /*Default constructor
		  *
		  */
	 FlightsTL:: Flight:: Flight (){
		   setOrigin("");
		   setDestination("");
		   setDepartureTime(0);
		   setDuration(0) ;


	 }

		  /* Constructor for flight object
		   *
		   *
		   */
 FlightsTL:: Flight::Flight(const string &orig ,const string &destin ,const double &dep, const double &dur ){
		org = orig;
		dest =  destin;
		depTime =dep;
		dura = dur;


	 }

		 /* copy constructor for flight object
		   *
		   *
		   *

		   FlightsTL:: Flight:: Flight(const Flight& rhs){



		   } */

		   /**destructor for flight object
		   *
		   *
		   */
		FlightsTL:: Flight::  ~Flight(){





		}
		   //end House-keeping functions
	   //______________________________________________\\

		   /* Equals operation
		   *
		   */
  FlightsTL::Flight& FlightsTL:: Flight::operator=(const Flight& rhs){

			return *this;
		 }

		   /* get origin
		   *
		   */
		 string FlightsTL:: Flight::getOrigin() const{

			  return org;
		   }

		   /* get destination
		   *
		   */
		 string FlightsTL:: Flight::getDestination()const{

			  return dest;



		   }

		   /* get departure time
		   *
		   */
		   double FlightsTL:: Flight:: getDepartureTime()const{



				return depTime;


		   }

		   /* get duration
		   *
		   */
		   double FlightsTL:: Flight:: getDuration ()const{



			   return dura;

		   }

		   /* set origin
		   *
		   */
		   void FlightsTL:: Flight:: setOrigin(string t ){
				  org = t;



		   }

		   /* set destination
		   *
		   */
		   void FlightsTL:: Flight:: setDestination(string s){

				  dest =s;

		   }

		   /* set departure time
		   *
		   */
		   void FlightsTL:: Flight:: setDepartureTime(double c){


			  depTime = c;


		   }

		   /* get duration
		   *
		   */
		   void FlightsTL:: Flight:: setDuration(double d){


				dura = d;


		   }

		   /* set all
		   *
		   */

		   void FlightsTL:: Flight:: setAll(string a,string b,double c,double d){

				  setOrigin(a);
				   setDestination(b);
				   setDepartureTime(c);
				   setDuration(d) ;



		   }




		   /** ToString to display flight information
		   *
		   */
		   string FlightsTL:: Flight:: toString()const{
				  string s;
				  double depT = getDepartureTime();
				  double durA =  getDuration();
				   // convert double to string
				  char deptStr[50];         
				  sprintf(deptStr,"%g",depT);
				  // convert to string
				  char durStr[50];
				  sprintf(durStr,"%g",durA);
				  // print all Flight information
				  s=getOrigin() +"\t"+ getDestination() + "\t"+ deptStr + "\t"+ durStr +"\n" ;
					return s;




		   }








	//__________________end of Flight implementation_____________\\


	/*Default constructor
		  *
		  */
		  FlightsTL::FlightsTL(){

				 FlightsNum = 0;

		  }

		  /* constructor
		  *
		  *
		  */

	   //	 FlightsTL:: FlightsTL(int num){



		// }

		  /*copy constructor
		  *
		  *
		  */

		 FlightsTL:: FlightsTL (const FlightsTL& rhs){
			 Flights = rhs.Flights;


		 }

		  /*destructor
		  *
		  *
		  */
		 FlightsTL:: ~FlightsTL(){



		 }

		  /*Equals operator
		  *
		  *
		  */
		 FlightsTL& FlightsTL::operator=(const FlightsTL& rhs){
					 if (this != &rhs){
						 return *this;
					 }

					return *this;
		 }

		  /* get FlightsNum
		   *
		   */
		 int  FlightsTL:: getFlightsNum (int num){


			   return FlightsNum;

		   }

		   /* set FlightsNum
		   *
		   */
		 //void FlightsTL::  setFlightsNum(int num){



		 // }


		   /*add Flighr
		   *
		   */
		 void FlightsTL:: addFlight(const string& origin , const string& destination,const double& depTime, const double& duration){

			  Flights.push_back(Flight(origin,destination,depTime,duration));
			  FlightsNum++;

		   }

			/** ToString to display all flights and thier information
		   *
		   */
		 string  FlightsTL:: toString()const{

			return 0;

		   }

I'd like to know how to instantiate the privately encapsulated class and the class that's encapsulating it in my testDriver. And if you notice anything else in the code which is bad please let me know.Thank you for help in advance:)

Jay F

Recommended Answers

All 10 Replies

> I have a class privately encapsulated in another class.
You can't access it except in the encapsulating class or friends of the encapsulating class. That's how private access works, and if you need to access the class outside of the encapsulating class or friends, then it shouldn't be private.

Is Your member a type FlightTl and i dont see any Declaration of a Flight type for acessing it.

Secondly , I dont know perfectly on this matter but i think that only a static class member can be a member of the same class .

CODEBLOCKS GIVES THIS A ERROR.

class hello
{
hello hello2;//This is an error According to my Compiler.
int x;
}

> I have a class privately encapsulated in another class.
You can't access it except in the encapsulating class or friends of the encapsulating class. That's how private access works, and if you need to access the class outside of the encapsulating class or friends, then it shouldn't be private.

then what do you suggest? i m lost.

How about making it public?

Is Your member a type FlightTl and i dont see any Declaration of a Flight type for acessing it.

Secondly , I dont know perfectly on this matter but i think that only a static class member can be a member of the same class .

CODEBLOCKS GIVES THIS A ERROR.

class hello
{
hello hello2;//This is an error According to my Compiler.
int x;
}

this was how i was trying to access the Flight class FlightsTL::Flight::FlightsTL:Flight::Flight b ; if this is not right then should i separate the two classes?

this the Header file:

#ifndef FLIGHTS_
#define FLIGHTS_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

class FlightsTL{

private:
class Flight {

		private:
		string org; //origin of flight
		string dest; //destination of flight
		double depTime;    // departure time
		double dura;  // duration of flight


		 public:
		   //Start House-keeping functions
		  /*Default constructor
		  *
		  */
		 Flight ();

		  /* Constructor for flight object
		   *
		   *
		   */
		 Flight(const string&, const string&,const double&,const double&);

		 /* copy constructor for flight object
		   *
		   *
		   */

		   Flight(const Flight& rhs);

		   /**destructor for flight object
		   *
		   *
		   */
		   ~Flight();
		   //end House-keeping functions
	   //______________________________________________\\

		   /* Equals operation
		   *
		   */
		   Flight& operator=(const Flight& rhs);

		   /* get origin
		   *
		   */
		   string getOrigin()const;

		   /* get destination
		   *
		   */
		   string getDestination()const;

		   /* get departure time
		   *
		   */
		   double getDepartureTime()const;

		   /* get duration
		   *
		   */
		   double getDuration ()const;

		   /* set origin
		   *
		   */
		   void setOrigin(string);

		   /* set destination
		   *
		   */
		   void setDestination(string);

		   /* set departure time
		   *
		   */
		   void setDepartureTime(double);

		   /* get duration
		   *
		   */
		   void setDuration(double);

		   /* set all
		   *
		   */

		   void setAll(string,string,double,double);

		   /** ToString to display flight information
		   *
		   */
		   string toString()const;






}  ;   // _________end of Flight class__________________\\

		friend class Flight;

		   private:
		   int FlightsNum; // keeps track of how many airports there are
			vector<Flight> Flights;   // container for all flights

			public:
			/*Default constructor
		  *
		  */
		  FlightsTL();

		  /* constructor
		  *
		  *
		  */

		  FlightsTL(int);

		  /*copy constructor
		  *
		  *
		  */

		  FlightsTL (const FlightsTL& rhs);

		  /*destructor
		  *
		  *
		  */
		  ~FlightsTL();

		  /*Equals operator
		  *
		  *
		  */
		  FlightsTL& operator=(const FlightsTL& rhs);

		  /* get FlightsNum
		   *
		   */
		   int getFlightsNum (int);

		   /* set FlightsNum
		   *
		   */
		   void setFlightsNum(int);

		   /*add Flighr
		   *
		   */
		   void addFlight(const string&, const string&,const double&, const double&);

			/** ToString to display flight information
		   *
		   */
		   string toString()const;


   } ;
#endif /*FLIGHTS_*/

i will try that! but how would i instantiate it?

Hey How About this thing.

Something like

class memclass
{
int y;
//.... All Functions are mentioned here for the Member Class
}

class bigclass
{
int x;
public://You can also keep memclass private and then acess it with a function.
memclass member;//The Member is now accessible using the . operator..
//Functions Continue And so on. 
}

thanks a lot, thats very insightful i will give it a try.

To access the member you will only need to do this.

int main()
{
bigclass sky;

sky.member::init();//That is if you have written a function like init.
}

Hey How About this thing.

Something like

class memclass
{
int y;
//.... All Functions are mentioned here for the Member Class
}

class bigclass
{
int x;
public://You can also keep memclass private and then acess it with a function.
memclass member;//The Member is now accessible using the . operator..
//Functions Continue And so on. 
}

Thank you for solving my problem, it worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.