0% found this document useful (0 votes)
13 views

05 - Structures and Classes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

05 - Structures and Classes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//Define a structure student which has 3 data members: id(int),

//score (float), and gender(char).


//Then write a program to read data of 3 students
//and print the id and gender of the student with bigest score
//Do not use any loop
/*
struct student
{
int id;
float score;
char gender;
};

struct student readData(void)


{
struct student temp;
cout << "Enter ID : ";
cin >> temp.id;
cout << "Enter score: ";
cin >> temp.score;
cout << "Enter gender: ";
cin >> temp.gender;
return temp;
}
void printData(struct student first)
{
cout << "The best student: ID: " << first.id << "\t score: "
<< first.score << " \t gender : " << first.gender << endl;
}

void main(void)
{
struct student a , b , c;
cout << "Enter data of first student:\n";
a = readData();
cout << "Enter data of second student:\n";
b = readData();
cout << "Enter data of third student:\n";
c = readData();

if((a.score > b.score) && (a.score > c.score)) printData(a);


else if(b.score > c.score)
printData(b);
else
printData(c);
}
*/

//Define a structure point, which has two float numbers x,y


// Define a class called line which has 2 data members of type points
// The class has function members: calculate_length
//read_data , set_data, get_p1, get_p2, print_data,
//, The initial values for x1,y1,x2,y2 = 0.
// Then write a program to make two lines, set first line
// data (line1) to be 10,10, 40, 50
// Read second line (line2) data from the user.
//Print which line is longer.

struct point
{
double x, y;
};

class line
{
private:
struct point p1 , p2;
double length;
public:
line(void) //constructor
{
p1.x = p1.y = p2.x = p2.y = 0;
}

line(double x1 , double y1 , double x2 , double y2) //constructor


{
p1.x = x1;
p1.y = y1 ;
p2.x = x2 ;
p2.y = y2 ;
}
void read_data(void)
{
cout << "Enter values of x1,y1, x2, y2: ";
cin >> p1.x >> p1.y >> p2.x >> p2.y ;
}

void set_data(double a, double b, double c, double d)


{
p1.x = a;
p1.y = b;
p2.x = c;
p2.y = d;
}
void set_x1(double a)
{
p1.x = a;
}
double get_x1(void)
{
return p1.x;
}
struct point get_p1(void)
{
return p1;
}
point get_p2(void)
{
return p2;
}
void print_data(void)
{
cout << "x1 = " << p1.x << "\t";
cout << "y1 = " << p1.y << "\t";
cout << "x2 = " << p2.x << "\t";
cout << "y2 = " << p2.y << "\n";
}

double get_length(void)
{
double dx, dy, length;
dx = pow ((p2.x - p1.x), 2.0);
dy = pow((double) (p2.y - p1.y), 2.0);
length = pow(dx + dy, 0.5);

return length;
}
};

/*
int main(void)
{
line line1 , line2(1,1,2,2);

cout << "Data of first line are: ";


line1.print_data();
cout << "Data of second line are: ";
line2.print_data();

line1.set_data(10,10,40,50);
line2.read_data();

cout << "Data of first line are: ";


line1.print_data();
cout << "Data of second line are: ";
line2.print_data();

struct point temp1 , temp2;


temp1 = line2.get_p1();
temp2 = line2.get_p2();
cout << "\t x1= " <<temp1.x << "\t y1 = " << temp1.y ;
cout << "\t x2= " <<temp2.x << "\t y2 = " << temp2.y << endl; ;

cout << "Length of first line = " << line1.get_length() << "\n";
cout << "Length of second line = " << line2.get_length() << "\n";
}
*/

You might also like