OOP Lab 8
OOP Lab 8
Static
”Student ”using static
class student
{
int id;
string name;
public:
void main()
static int st_num; {
student(int id, string name) student s1( 1, “ali”);
{ cout<< student:: st_num <<endl;
st_num++;
student s2 ( 5 , “ahmed”);
this->id= id; cout<< student:: st_num <<endl;
this->name= name;
} student s3( 3, “mohamed”);
}; cout<< student:: st_num <<endl;
}
int student :: st_num=0;
Student ”using static” (cont.)
class student
{ int student :: st_num=0;
int id;
string name;
static int st_num;
public: void main()
student(int id, string name) {
student s1( 1, “ali”);
{
cout<< student:: getStNum() <<endl;
st_num++;
this->id= id; student s2( 2 , “ahmed”);
this->name= name; cout<< student:: getStNum() <<endl;
}
student s3( 3, “mohamed”);
static int getStNum()
cout<< student:: getStNum() <<endl;
{
return st_num; }
}
}; // end of student class
Exercise 1
A corporation has three divisions, each responsible for sales to different
geographic locations. Design a DivSales class that keeps sales data for a
division, with the following
data members:
first_half, second_half, and static variable total_sales
The class also contains the following member functions:
• A member function that takes two arguments, each assumed to be the
sales for a half. The value of the arguments should be copied into the
variables hold the sales data. The total of the two arguments should
be added to the static variable that holds the total yearly corporate
sales.
Write a program that creates an array of three DivSales objects. The
program should ask the user to enter the sales for the two half for each
division. After the data are entered, the program should display a table
showing the division sales for each half. The program should then display
the total corporate sales for the year.
Friend Function
A friend is a way to contain similar methods and ideas that
are outside the scope of the original class, but are reliant on
void main()
{
square sq(5);
rect rec(3,5);
cout << addArea( sq , rec) <<endl;
}
Example ”using set & get”
class square class rect
{ {
int side; int width, height;
public: public:
square(int a) rect (int a, int b)
{ {
side = a; width = a;
} height = b;
int getSide() }
{ int getWidth() { return width; }
return side; int getHeight() { return height; }
} };
};
Example ”using set & get” (cont.)
void main()
{
square sq(5);
rect rec(3,5);
cout << addArea( sq , rec) <<endl;
}
Friend Function
This situation arises mostly in case of operator overloading.
When you want to compare two private data members of two
different classes. In that case you create a normal function
and make friend in both the classes, as to provide access of
theirs private variables.
”Example ”using multiple friend
class rect;
class rect
class square {
{ int width, height;
int side; public:
public: rect (int a, int b)
square(int a) {
{ width = a;
side = a; height = b;
} }
friend int addArea(square s, rect r); friend int addArea(square s, rect r);
}; };
Example ”using multiple friend” (cont.)
void main()
{
square sq(5);
rect rec(3,5);
cout << addArea( sq , rec) <<endl;
}
Exercises 2
Use operator overloading instead of addArea.
;dis.displayItem(st)
}