Professional Documents
Culture Documents
Oop Lab 7
Oop Lab 7
Software Engineering
FJWU-2022-BSE-066
Lecturer
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;
}
// 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);
// 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>
// 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;
}
// 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>
// 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>
int main() {
Point p1(1, 2);
Point p2(3, 4);
Point p3(5, 6);
// Personal details
cout << "Name: Sadia\n";
cout << "Reg. No: 66\n";
cout << "Section: B\n";
cout << "Semester: 2nd\n";
return 0;
}