Jump to content

I need some deep c++ object knowledge!

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Turambar

Turambar

    Newbie

  • Members
  • PipPip
  • 10 posts
Hello everybody!

My teacher gave me a homework assignment a week ago. I have been trying to create two classes one of them is Flight and the other one is Plane.
I have created nearly all of them but I can't go on.:cursing: I'm putting my code here so if any one willing to help me ( tell me how to do it ) I would be greatly thankful. ^^

The commend parts are the ones that I was not able to complete/understand. Some of them are false written but I still put them there so that you get the idea.

#include <cstdlib>

#include <iostream>

#include <string>


using namespace std;


class Plane

{

public:

	Plane( string, int, int );

	void display();

	//Plane select(); //displays the plane list and let user select one


private:   

	static const int MAXPLANE = 40; 

	//Plane planes[MAXPLANE]; //it saves Plane objects

	//int plncnt; //counts the number of planes 

	string type;

	int passenger;

	int distance;


};


Plane::Plane( string _type, int _passenger, int _distance )

{

	type = _type;

	passenger = _passenger;

	distance = _distance;

}

void Plane::display()

{

	cout << "Type : " << type << endl;

	cout << "Passenger Number : " << passenger << endl;

	cout << "Distance : " << distance << endl;

}


/*

Plane Plane::select()

{

  ??

}

*/


class Flight

{

public:

	Flight( string, string, string, string );

	//void setPlane(); //set plane object (call select method of plane)

	void setTime(string);

	void displayFrom(string);


private:

	static const int MAXFLIGHT = 35; 

	//Flight flights [MAXFLIGHT];// it saves Flight objects

	//int flycnt; // counts the number of flights 

	string flightno;

	string from;

	string to;

	string time;

};


Flight::Flight( string _fligtno, string _from, string _to, string _time )

{

	flightno = _fligtno;

	from = _from;

	to = _to;

	time = _time;

}

/*

void Flight::setPlane()

{

	??

}

*/

void Flight::setTime(string _time)

{

	time = _time;

}


/*

void Flight::displayFrom(string)

{

	??

}

*/


int main()

{

	Plane p1("Oceanic815",34,567);

	p1.display();

	system("PAUSE");

	return EXIT_SUCCESS;

}


#2
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
My notes:
  • There's a problem with how you're thinking about the two classes Plane and Flight.
    In both, you're thinking about containing an array of objects in an object of the same
    type. I'd advice you to think more in terms of abstractions; a plane doesn't contain
    other planes.
  • A lot of problems will become easier once the abstractions are more closer to real
    world concepts. Plane has a count of passengers, but it intrinsically doesn't have a
    distance unless it's the distance that a particular plane has travelled in its existence.
    A type a plane has, of course.
  • Once you create, lets say a class called Planes, use vector or one of the other std
    containers. As it appears that you're a beginner, just replace arrays with vectors.
    There's really no reason to use arrays unless you know at compile time how many
    objects will be contained. Sometimes performance matters but mostly it doesn't.


#3
Turambar

Turambar

    Newbie

  • Members
  • PipPip
  • 10 posts

denarced said:

My notes:
  • There's a problem with how you're thinking about the two classes Plane and Flight.
    In both, you're thinking about containing an array of objects in an object of the same
    type. I'd advice you to think more in terms of abstractions; a plane doesn't contain
    other planes.
  • A lot of problems will become easier once the abstractions are more closer to real
    world concepts. Plane has a count of passengers, but it intrinsically doesn't have a
    distance unless it's the distance that a particular plane has travelled in its existence.
    A type a plane has, of course.
  • Once you create, lets say a class called Planes, use vector or one of the other std
    containers. As it appears that you're a beginner, just replace arrays with vectors.
    There's really no reason to use arrays unless you know at compile time how many
    objects will be contained. Sometimes performance matters but mostly it doesn't.

Thanks for the reply!

1- How do I make a class save it's objects?
2- Sadly the abstractions are not mine but my teachers so I'll have to go on with them.
3- I will check out vectors. I never used them actually. I have always used arrays.

#4
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts

Turambar said:

Thanks for the reply!

1- How do I make a class save it's objects?
2- Sadly the abstractions are not mine but my teachers so I'll have to go on with them.
3- I will check out vectors. I never used them actually. I have always used arrays.
You don't make a class save its objects, it's conceptually problematic. A class is a blueprint,
from which objects are created. While classes are in some languages considered objects, I
believe not in C++.

#5
Turambar

Turambar

    Newbie

  • Members
  • PipPip
  • 10 posts
Ok thanks. I think I have the informations wrong. I'll speak with my teacher again. Thank you very much.

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Plane is a data type and instance of it is an object. In example code, Car is class and toyota is an object:
class Car {
};

int main() {
    Car toyota;
}
It'd be nice if you could provide instructions as well, so we can help you further.

If you want to keep a record of how many planes you have, use static keyword. static has several meanings and in context of a property (member variable) it mean's it's shared with other objects of same type.
class Plane {
    public:
        Plane(std::string, int, int);

    private:
        static unsigned int numberOfPlanes_;
};

// initialize plane counter
unsigned int Plane::numberOfPlanes_ = 0;

// use initialization lists in constructors
Plane::Plane(std::string a, int b, int c)
    : type(a), passenger(b), distance(c)
{
    numberOfPlanes_++;
}

// don't forget to decrement it after plane is [I]gone[/I]
Plane::~Plane() {
    numberOfPlanes_--;
}

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
Turambar

Turambar

    Newbie

  • Members
  • PipPip
  • 10 posts
Flying Dutchman Thank you for your information. I have been studying static keyword after I read your reply. I'm making some progress right now. Thanks for your help.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users