15-ClassComposition

15-ClassComposition - {} }; int main (void) { Line...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> using namespace std; class Point { private: float x, y; public: Point(float x1 = 0.0f, float y1 = 0.0f) { setCoordinates(x1, y1); cout << "Point built: "; print(); } void setCoordinates(float x1, float y1) { x = x1; y = y1; } float getX() const { return x; } float getY() const { return y; } void print() const { cout << "(" << x << ", " << y << ")" << endl; } }; class Line { private: Point start, end; public: Line(float x1, float y1, float x2, float y2) : start(x1, y1), end(x2, y2) { cout << "Line built\n"; //start(1,2); } }; class Line1 { private: Point start/*(7, 8)*/; // we cannot initialize class level variables like this Point end/*(4, 5)*/; public: Line1(float x1 = 0.0f, float y1 = 0.0f, float x2 = 0.0f, float y2 = 0.0f) : start(x1, y1), end(x2, y2) {} }; int returnAnInt() {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
return 0; } class PublicLine { public: Point start; Point end; PublicLine() :start(returnAnInt(),0), end(0,0)
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: {} }; int main (void) { Line aLine(1,2,3,4); PublicLine pline; cout << "the start member of the PublicLine object " << "is accessable from the main, but the x " << "member of the start object is still private " << "and therefore can only be accessed from within " << "the Point class\n"; //cout << &aLine.end << endl; //error cannot access private variable cout << &pline.start << endl; pline.start.getX(); //pline.start.x = 5; return 0; } /* Point built: (1, 2) Point built: (3, 4) Line built Point built: (0, 0) Point built: (0, 0) the start member of the PublicLine object is accessable from the main, but the x member of the start object is still private and therefore can only be accessed from within the Point class 0012FF3C Press any key to continue . . . */...
View Full Document

This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

Page1 / 2

15-ClassComposition - {} }; int main (void) { Line...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online