You are on page 1of 6

Lab#07- Composition

Submitted by: Sadia Bibi

Software Engineering

FJWU-2022-BSE-066

Submitted to: Engr. Rehan Ahmed

Lecturer

Fatima Jinnah Women’s University

Department of Software Engineering


Fatima Jinnah Women’s University
Task:01
Create a class Point with two data members x, y. Provide appropriate constructors,  get,
set and display methods. 
Create a class Triangle with three Points as its data members. Provide appropriate 
constructors for this class and a display method which calls the display methods of  the
three Points. 
In the main function, declare three points and pass them to the constructor of the  class
Triangle. Call the display method of Triangle to verify the coordinates of the  triangle. 
Print your name, reg,no, section, semester in main using co ut statements. 
  
#include <iostream>
using namespace std;

class Point {
private:
int x, y;

public:
// Default constructor
Point() {
x = 0;
y = 0;
}

// Parameterized constructor
Point(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;
}

// Get method
int getX() {
return x;
}

int getY() {
return y;
}

// Setter methods
void setX(int xCoord) {
x = xCoord;
}

void setY(int yCoord) {


y = yCoord;
}

// Display method
void display() {
cout << "(" << x << ", " << y << ")";
}
};

class Triangle {
private:
Point point1, point2, point3;

public:
// Parameterized constructor
Triangle(Point p1, Point p2, Point p3) {
point1 = p1;
point2 = p2;
point3 = p3;
}

// Display method
void display() {
cout << "Triangle coordinates:\n";
cout << "Point 1: ";
point1.display();
cout << endl;
cout << "Point 2: ";
point2.display();
cout << endl;
cout << "Point 3: ";
point3.display();
cout << endl;
}
};

int main() {
Point p1(1, 2);
Point p2(3, 4);
Point p3(5, 6);

Triangle triangle(p1, p2, p3);


triangle.display();

// personal details
cout << "Name: Sadia\n";
cout << "Reg. No: 66\n";
cout << "Section: B\n";
cout << "Semester: 2nd\n";
return 0;
}
TASK:02
Create separate header file(s) for the above code for each of the class definition and 
separate .cpp file four your main()
point.h:
class Point {
private:
int x, y;

public:
// Default constructor
Point();

// Parameterized constructor
Point(int xCoord, int yCoord);

// Get methods
int getX();
int getY();

// Setter methods
void setX(int xCoord);
void setY(int yCoord);

// Display method
void display();
};

point.cpp:

#include "point.h"
#include <iostream>

using namespace std;

// Default constructor
Point::Point() {
x = 0;
y = 0;
}
// Parameterized constructor
Point::Point(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;
}

// Get methods
int Point::getX() {
return x;
}

int Point::getY() {
return y;
}

// Setter methods
void Point::setX(int xCoord) {
x = xCoord;
}

void Point::setY(int yCoord) {


y = yCoord;
}

// Display method
void Point::display() {
cout << "(" << x << ", " << y << ")";
}
triangle.h:

cpp
Copy code
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "point.h"

class Triangle {
private:
Point point1, point2, point3;

public:
// Parameterized constructor
Triangle(Point p1, Point p2, Point p3);

// Display method
void display();
};

triangle.cpp:

#include "triangle.h"
#include <iostream>

using namespace std;

// Parameterized constructor
Triangle::Triangle(Point p1, Point p2, Point p3) {
point1 = p1;
point2 = p2;
point3 = p3;
}

// Display method
void Triangle::display() {
cout << "Triangle coordinates:\n";
cout << "Point 1: ";
point1.display();
cout << endl;
cout << "Point 2: ";
point2.display();
cout << endl;
cout << "Point 3: ";
point3.display();
cout << endl;
}

main.cpp:

#include "point.h"
#include "triangle.h"
#include <iostream>

using namespace std;

int main() {
Point p1(1, 2);
Point p2(3, 4);
Point p3(5, 6);

Triangle triangle(p1, p2, p3);


triangle.display();

// Personal details
cout << "Name: Sadia\n";
cout << "Reg. No: 66\n";
cout << "Section: B\n";
cout << "Semester: 2nd\n";
return 0;
}

You might also like