Lab 11 Classes and Objects

advertisement
CS2311 Computer Programming
Lab 11
Classes and Objects
Q1. Write a class having two public member variables (e.g., int x, y) and one member
function which will return the area of the rectangle. Please finish the following program and
test it (on your PC).
#include <iostream>
using namespace std;
class MyRectangle
{
public:
int
int
};
;
{return
;};
void main()
{
MyRectangle
;
cout << "Enter height of rectangle:";
cin >>
;
cout << "Enter width of rectangle:";
cin >>
;
cout << "Area:" <<
<endl;
}
Q2. Write a program which reads in two integers in the main function and passes them to
default constructor of the class named data. Show the result of the addition of two numbers.
Please finish the following program and test it (on your PC).
// Exercises: Classes
// Exercise 2
#include <iostream>
using namespace std;
class data{
public:
int
data(
int
};
data::data(
{
;
);
{ return
;}
)
=
=
cout << "numbers initialized \n";
;
;
}
-1-
CS2311 Computer Programming
void main()
{
int num1;
int num2;
cout << "Enter first number : ";
cin >> num1;
cout << "Enter second number : ";
cin >> num2;
;
cout << "The addition result on:";
cout <<
<<endl;
}
Q3. Write a c++ class called “Student”. The program asks the user to enter the name and
mark of a student. It use member functions compute_average_mark() to calculate the
average mark and display() to display the name and average mark on the screen in different
lines. Please finish the following program and test it (on your PC).
#include <iostream>
using namespace std;
class _____________
{
public:
char *name;
int mark1,mark2;
Student(
{
)
=
=
=
}
double compute_average_mark()
{
return
}
void display()
{
cout<<"Student name:"<<
cout <<"Average:"<<
}
;
;
;
;
<<endl;
()<<endl;
};
-2-
CS2311 Computer Programming
void main()
{
char name[30];
int make1, make2;
cout << "Enter name:";
cin >> name;
cout << "Enter marks of two subjects:";
cin >> make1;
cin >> make2;
________________________;
________________________;
}
Q4. Perform addition operation on complex data using class and object. The program should
ask for real and imaginary part of two complex numbers, and display the real and imaginary
parts of their sum.
#include <iostream>
using namespace std;
class MyComplex
{
public:
double real, imaginary;
MyComplex(________________)
{
real = ________________;
imaginary = ________________;
}
};
void main()
{
double number1, number2;
cout << "first number \n";
cout << "Enter the real part:";
cin >> number1;
cout << "Enterthe imaginary part:";
cin >> number2;
MyComplex __________________________________;
cout << "second number \n";
double number3, number4;
cout << "Enter the real part:";
cin >> number3;
cout << "Enterthe imaginary part:";
cin >> number4;
_____________________________________________;
double real;
real = __________________________________;
double imagin;
imagin = __________________________________;
cout << "the sum of the real parts is " << real <<endl;
cout << "the sum of the imaginary parts is " << imagin<<endl;
}
-3-
Download