15-ClassComposition - int main(void Line aLine(1,2,3,4...

Info icon This 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() {
Image of page 1

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

View Full Document Right Arrow Icon
return 0; } class PublicLine { public: Point start; Point end; PublicLine() :start(returnAnInt(),0), end(0,0)
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern