- Back to Home »
- Assignment-3 NUST OOP in C++
Posted by : Unknown
Tuesday, 31 March 2015
Write a class “Line” with following
specifications
• three data members, endp1, endp2 and char
pointer linetype (Give any character value in linetype e.g B implies bold line,
N implies normal line and S implies straight line)
• default constructor with default arguments
• copy constructor
• Write getter & setter functions.
• calculateLength method which calculates the
length of the line
• compareLine method which compares two line
objects and tells which line is longer (use this pointer to use the current
objects data members, you can pass 2nd object in the parameter of
this function)
• In main program
} Create line1 object so that default
constructor is called
} Create line2 object using line1 object (copy constructor must be called)
} Calculate the length of each line object and
compare them by calling functions.
#include<iostream>
#include<conio.h>
using namespace std;
class line{
private:
int endp1;
int endp2;
char *linetype;
public:
line(int x = 0, int y = 20, char z = 'B'){
endp1 = x;
endp2 = y;
linetype = new char;
*linetype = z;
}
line(const line& l1){
this->endp1 = l1.endp1;
this->endp2 = l1.endp2;
this->linetype = new char;
*linetype = *l1.linetype;
}
void setVal(int x, int y, char z){
endp1 = x;
endp2 = y;
*linetype = z;
}
void getVal(){
cout << "Enp1: " << endp1 << endl<< "Endp2: " << endp2 << endl << "Line Type: " << *linetype << endl;
}
void calcLength(){
cout << "Length of line is: " << endp2 - endp1 << endl;
}
void compareL(line &l1){
if ((this->endp2 - this->endp1) > (l1.endp2 - l1.endp1)){
cout << "Line is greater then the sended line";
}
else if ((this->endp2 - this->endp1) < (l1.endp2 - l1.endp1)){
cout << "Line is smaller then the sended line";
}
else{
cout << "Lines are equal";
}
}
};
void main(){
line l1;
line l2(l1);
l1.getVal();
l1.calcLength();
cout << endl << endl;
l2.getVal();
l2.calcLength();
cout << endl<< "Setting l2 new values" << endl;
l2.setVal(10, 15, 'N');
l2.getVal();
l2.calcLength();
cout << endl << "Comparing l1 and l2" << endl;
l1.compareL(l2);
_getch();
}
#include<conio.h>
using namespace std;
class line{
private:
int endp1;
int endp2;
char *linetype;
public:
line(int x = 0, int y = 20, char z = 'B'){
endp1 = x;
endp2 = y;
linetype = new char;
*linetype = z;
}
line(const line& l1){
this->endp1 = l1.endp1;
this->endp2 = l1.endp2;
this->linetype = new char;
*linetype = *l1.linetype;
}
void setVal(int x, int y, char z){
endp1 = x;
endp2 = y;
*linetype = z;
}
void getVal(){
cout << "Enp1: " << endp1 << endl<< "Endp2: " << endp2 << endl << "Line Type: " << *linetype << endl;
}
void calcLength(){
cout << "Length of line is: " << endp2 - endp1 << endl;
}
void compareL(line &l1){
if ((this->endp2 - this->endp1) > (l1.endp2 - l1.endp1)){
cout << "Line is greater then the sended line";
}
else if ((this->endp2 - this->endp1) < (l1.endp2 - l1.endp1)){
cout << "Line is smaller then the sended line";
}
else{
cout << "Lines are equal";
}
}
};
void main(){
line l1;
line l2(l1);
l1.getVal();
l1.calcLength();
cout << endl << endl;
l2.getVal();
l2.calcLength();
cout << endl<< "Setting l2 new values" << endl;
l2.setVal(10, 15, 'N');
l2.getVal();
l2.calcLength();
cout << endl << "Comparing l1 and l2" << endl;
l1.compareL(l2);
_getch();
}