Oops, Quiz Solution - Write code for these two classes such that the following code, Quizzes of Object Oriented Programming

A line is formed using two points. Create two classes called Line and Point. Point class should only have two variables called x and y. Write code for these two classes such that the following code in main() can be run: int main() { Line newLine; newLine.set1stPoint(0,0); newLine.set2ndPoint(10,10); cout << newLine.getSlope(); return 0; } The function getSlope() should return the slope of a line using the formula (y2 − y1) / (x2 − x1). Define the function getSlope() outside the class.

Typology: Quizzes

2022/2023

Available from 12/09/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Q.1 (Marks 10)
A line is formed using two points. Create two classes called Line and Point. Point class should only have two
variables called x and y. Write code for these two classes such that the following code in main() can be run: int
main()
{
Line newLine;
newLine.set1stPoint(0,0);
newLine.set2ndPoint(10,10);
cout << newLine.getSlope();
return 0;
}
The function getSlope() should return the slope of a line using the formula (y2 − y1) / (x2 − x1). Define the
function getSlope() outside the class.
Ans:
#include<iostream>
using namespace std;
class Point
{
public:
int x1;
int x2;
int y1;
int y2;
int c;
int set1stPoint(int x11,int y11){
x1=x11;
y1=y11;
}
int set2ndPoint(int x22,int y22) {
x2=x22;
y2=y22; }
};
class Line:public Point
{
public:
int getslope() {
c = (y2-y1) / (x2-x1);
return c;
}
};
int main()
{
Line newLine;
newLine.set1stPoint(0,0);
newLine.set2ndPoint(10,10);
cout << newLine.getslope();
return 0;
pf2

Partial preview of the text

Download Oops, Quiz Solution - Write code for these two classes such that the following code and more Quizzes Object Oriented Programming in PDF only on Docsity!

Q.1 (Marks 10) A line is formed using two points. Create two classes called Line and Point. Point class should only have two variables called x and y. Write code for these two classes such that the following code in main() can be run: int main() { Line newLine; newLine.set1stPoint(0,0); newLine.set2ndPoint(10,10); cout << newLine.getSlope(); return 0; } The function getSlope() should return the slope of a line using the formula (y2 − y1) / (x2 − x1). Define the function getSlope() outside the class. Ans: #include using namespace std; class Point { public: int x1; int x2; int y1; int y2; int c; int set1stPoint(int x11,int y11){ x1=x11; y1=y11; } int set2ndPoint(int x22,int y22) { x2=x22; y2=y22; } }; class Line:public Point { public: int getslope() { c = (y2-y1) / (x2-x1); return c; } }; int main() { Line newLine; newLine.set1stPoint(0,0); newLine.set2ndPoint(10,10); cout << newLine.getslope(); return 0;

Object Oriented Programming Page 1 of 2