Dear all, I would like to ask you how to initialize the following:

I want to initialize a class inside another class.
How I will be able to do so?

An example:

/*Class Date*/
class Date {
	public:
		Date(){} // default constructor
		int day;//day of the month valid till 31
		int month; //month of the year , valid till the 12
		int year;//year 
		
		/* function prototypes*/
		void setDate(int d, int m, int y);
		void printDate();
};
	/*functions*/
	void Date::setDate(int d, int m, int y){day=d;month=m;year=y;}// i must vallidate entries for days months etc
	void Date::printDate(){cout <<day<< "/" << month << "/" << year <<endl;}
/*end class Date*/

This is the class I want to include in another class implementation
I want to include this in the following class:

/*Class Flights*/
class Flights{
	public:
	Flights(){}
	string flight_code;
	string departure;
	string destination;
	FlightOperators FOperator;
	int seats; // avaliable seats based on the flight
	int meals; // available meals 
	int drinks; //available drinks in the flight
	Date departuredate; //departure date of type class Date
	Time departuretime; // departure time of type class Time
	//functions 
	void addFlights(string FC, string Dp,string Ds, int s,int dd);
	void getFlights();
	void searchFlights();
	void updateFlights();
};
void Flights::addFlights(string FC, string Dp,string Ds, int s,int dd){flight_code=FC;departure=Dp;destination=Ds;}
void Flights::getFlights(){cout<<"Get flights"<<endl;}
void Flights::searchFlights(){cout<<"Search flights"<<endl;}
void Flights::updateFlights(){cout<<"Updateflights"<<endl;}
/*end Class Flights*/

I will create a member function inside flights?
how I am going to implement this???

I also have another function to initialize 150 array objects of class flights

Flights *FL=new Flights[150];
FL[0] ....??????

Thanks

Do you want to use the object of 'Flights' inside the same class?

if, yes

Follow the following outline:

Class Flights
{
public:
Flights *FL;
.......
.......
void function() // use a non constructor function for this
{
FL=new Flights[150];
........
}
........
};

Do you want to use the object of 'Flights' inside the same class?

if, yes

Follow the following outline:

Class Flights
{
public:
Flights *FL;
.......
.......
void function() // use a non constructor function for this
{
FL=new Flights[150];
........
}
........
};

I need to use Date class and initialize it into flights class.
Is this possible or I need to implement somethig else?

First use code tags.

Class Flights
{
public:
Flights *FL;
.......
.......
void function() // use a non constructor function for this
{
FL=new Flights[150];
........
}
........
};

You may also want to consider using a struct if it fits your needs. They are simple to put into a class.

You can include an object of class Date into Flights just like any other data types. Your initialization of the Flight classes are correct.

You can include an object of class Date into Flights just like any other data types. Your initialization of the Flight classes are correct.

So how I am going to initialize the data class inside flights?

Flights *FL=new Flights[150];// use this to create 150 oblects of Flights
//to initialize
Flights[0].departuredate.setDate; //I will use something like this?

Your Flights class is already capable of handling the Date class

use following declaration

Flights FL[150];
Flights[0].departuredate.setDate;

or

Flights *FL=new Flights[150];
(Flights+0)->departuredate.setDate;

Your Flights class is already capable of handling the Date class

use following declaration

Flights FL[150];
Flights[0].departuredate.setDate;

or

Flights *FL=new Flights[150];
(Flights+0)->departuredate.setDate;

Many thanks
Thats clears a lot of things.
Many many thanks
Wish to all of you Happy and Prosperous New Year.

This article has been dead for over six months. Start a new discussion instead.