Archive
The publication, book and tape classes , more advance….
Add another base class sales that holds and array of three floats it can record the sales of a particular publication for the last three months. Include getdata() function to get three sales amounts from the user , and a putdata() function to display the sales figures. Alter the book and tape classes so that they are derived from both publication and sales. An object of class book or tape should input or output sales along with its other data.
#include
using namespace std;
class sale
{
private:
float salerec[3];
public:
void getdata()
{
cout << " Enter the sales for the last three months :" << endl;
for (int i = 0; i < 3 ; i++)
{
cout << " Enter sales for month : " << i+1 << " > “;
cin >> salerec[i];
}
}
void putdata()
{
cout << " The sale of the last three month " << endl;
for(int i=0; i < 3; i++)
{
cout << " Month no : " << i+1 << " > ” << salerec[i] << endl;
}
}
};
class publication
{
private:
char title[50] ;
float price;
public:
void getdata()
{
cout << " Enter publication title : " ;
cin >> title;
cout << " Enter publication price : " ;
cin >> price;
}
void putdata()
{
cout << " Publication title : " << title << endl;
cout << " Publication price : " << price << endl;
}
}; // end of class publication
class book : public publication, public sale
{
private:
int pageCount ;
public:
void getdata()
{
publication::getdata();
cout << " Enter page count : " ;
cin >> pageCount;
sale::getdata();
}
void putdata()
{
publication::putdata();
cout << " Page count : " << pageCount ;
cout << endl;
sale::putdata();
}
};
class tape : public publication , public sale
{
private:
int playingtime;
public:
void getdata()
{
publication::getdata();
cout << " Enter page count : ";
cin >> playingtime;
sale::getdata();
}
void putdata()
{
publication::putdata();
cout << " Playing time : " << playingtime;
cout << endl;
sale::putdata();
}
};
int main(int argc, char** argv) {
book mybook;
//tape mytape;
// get the book data
mybook.getdata();
//mybook.sale::getdata();
mybook.putdata();
//mybook.sale::putdata();
// get the tape data
//mytape.getdata();
//mytape.putdata();
return (EXIT_SUCCESS);
}
[/sourcecode]
Inheritance , Classroom Exercise, NIIT student
Create a class called publication that store the title ( a string ) and price ( type float ) of a publication. From this class, derive two class: book which adds a page count ( type int ); and tape , which adds a playing time (type int ) in minutes. Each of these classes should have a getdata() function to get its data from the user, and a putdata() to display its data.
//———————————————————
#include
using namespace std;
class publication
{
private:
char title[50] ;
float price;
public:
void getdata()
{
cout << " Enter publication title : " ;
cin >> title;
cout << " Enter publication price : " ;
cin >> price;
}
void putdata()
{
cout << " Publication title : " << title << endl;
cout << " Publication price : " << price << endl;
}
}; // end of class publication
class book : public publication
{
private:
int pageCount ;
public:
void getdata()
{
publication::getdata();
cout << " Enter page count : " ;
cin >> pageCount;
}
void putdata()
{
publication::putdata();
cout << " Page count : " << pageCount ;
cout << endl;
}
};
class tape : public publication
{
private:
int playingtime;
public:
void getdata()
{
publication::getdata();
cout << " Enter page count : ";
cin >> playingtime;
}
void putdata()
{
publication::putdata();
cout << " Playing time : " << playingtime;
cout << endl;
}
};
int main(int argc, char** argv) {
book mybook;
tape mytape;
// get the book data
mybook.getdata();
mybook.putdata();
// get the tape data
mytape.getdata();
mytape.putdata();
return (EXIT_SUCCESS);
}
[/sourcecode]
.. That what i've done with the exercise ...
Filled dimon or UnFilled dimon, OOPs Technique..
#include <iostream.h> using namespace std; class dimon { private: int size; public: dimon() { size = 0; } void setSize(int isize) { size = isize; } int getsize() { return size; } void pWhiteSpace(int num ) // draw the white space { for(int i=0; i<num ; i++) { cout << " " ; } } void pStar(int num) // draw the star "*" { for(int i=0; i<num; i++) { cout << "*" ; } } }; class FillDimon : public dimon { public: void show() { int size = getsize() ; int middle = size / 2; int j = middle ; for(int i=0; i < size ; i++) { if(i<=middle) { pWhiteSpace(j--); pStar(i); pStar(i+1); } else { pWhiteSpace(++j+1); pStar(size - i); pStar(size - i -1 ); } cout << endl; } } }; class UnFillDimon : public dimon { private: void pSW(int num) { int whitenum; if(num==1) { cout << "*"; } else if(num >1 ) { whitenum = num - 2; cout << "*"; pWhiteSpace(whitenum); pWhiteSpace(whitenum + 1); cout << "*"; } } public: void show() { int size = getsize(); int middle = size/2; int j = middle; for(int i = 0; i<size; i++) { if(i <= middle) { pWhiteSpace(j--); pSW(i+1); } else { pWhiteSpace(++j+1); pSW(size-i); } cout << endl; } } }; int main(int argc, char** argv) { int ch; FillDimon mydimon; UnFillDimon myudimon; cout << " Enter number 1 or 2 to display Filled Dimon or UnFilled Dimon :" << endl; cout << " >> " ; cin >> ch ; if ( ch == 1 ) { mydimon.setSize(25); mydimon.show(); } else { myudimon.setSize(25); myudimon.show(); } return (EXIT_SUCCESS); }
This is another version of Filled / Unfilled Dimon , using OOP.
and try to use inheritance , base class , derive class. . . . have fun
Print Filled diamon or un Filled Diamon
#include
using namespace std;
void pFilledDimon(int size);
void pUnFilledDimon(int size);
void pWhiteSpace(int num); // print the white space , by amount of num
void pStar(int num); // print the * , by amount of num
void pSW(int num);
int main(int argc, char *argv[])
{
int choice;
system(“cls”);
cout << "Enter the number for Filled Dimon or UnFilled Dimon (1 or 2) : " ;
cin >> choice ;
cout << endl;
if (choice == 1)
{
pFilledDimon(25);
}
else
{
pUnFilledDimon(25);
}
cout << endl;
cout << "Thank you for using me ! " << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void pFilledDimon(int size)
{
int middle = size / 2;
int j = middle;
for (int i=0; i
{
whitenum = num – 2;
cout << "*";
pWhiteSpace(whitenum);
pWhiteSpace(whitenum+1);
cout << "*";
}
}
[/sourcecode]
This is just another exercise. using fuction and algorithm to print the dimon shap with "*" , and display filled and unfilled dimon indentify by user.
Simple sort
#include
using namespace std;
const int Max = 5;
void dcOrder(int []);
void acOrder(int []);
void swap(int *, int *);
int main(int argc, char *argv[])
{
int iarray[Max] ;
cout << " Enter five numbers " << endl ;
for (int i = 0; i < Max ; i++)
{
cout <> “;
cin >> iarray[i] ;
}
acOrder(iarray);
//dcOrder(iarray);
cout << endl;
cout << "The new Order according to the sorting function is " << endl;
for(int i = 0; i < Max ; i++)
{
cout < " << iarray[i] << endl;
}
//system("PAUSE");
//return EXIT_SUCCESS;
return 0;
}
void dcOrder(int iarray [])
{
for ( int i = 0; i < Max ; i++)
{
int tempnum = iarray[i];
int j = i ;
while (j tempnum)
{
swap(&iarray[j],&tempnum);
}
j++;
}
iarray[i]=tempnum;
}
}
void acOrder(int iarray [])
{
for ( int i = 0; i < Max ; i++)
{
int tempnum = iarray[i];
int j = i ;
while (j < Max)
{
if (iarray[j] < tempnum)
{
swap(&iarray[j],&tempnum);
}
j++;
}
iarray[i]=tempnum;
}
}
void swap(int *n1, int *n2)
{
int tempnum;
tempnum = *n1;
*n1 = *n2;
*n2 = tempnum;
}
[/sourcecode]
We learn many thing in this exercise. First we learn how to work with function. we learn how to pass parameters to the function , pass by reference. Function dcOrder and acOrder are the function that to do the sorting in descending and accending order . They accept an array parameter. when we call this function we pass the name of array to the function and what happend the fucntion will effect to the input parameter . that mean the value of array will change according to the function.
The same way in the swap(int *, int *) function. i us the pointer technique to make the value swap between the two input parameters. or you can use the same technique but different manner , by using swap(int & , int & ) instead. and it will look like this
void swap(int &num1, int &num2)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
to call this function just pass the variable name ...
it sound better , isn't it.?
in additional , Passing arrays to functions , the name of array variable is actually the address of the first element of the array[0] , The parameter of the called function, some_list , can be declared in any of the following ways:
functionName(int some_list []) // with no subscript
{ .... }
OR
functionName(int some_list[10]) // same size as the array you want to pass to function
{..... }
OR
functionName(int *some_list) // int type pointer
{.....}
Since the address of the array is always passed to a function , it is easy to alter the contents of the array directly in the function. Therefore, arrays do not have to be returned from function explicitly the return statement.