Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Today's Agenda:
- 1) Overloading normal method
- 2) Method chaining
- 3) Overloading operators
- 4) Brainstorming sesson on project milestone-1
- ==============================================
- class ComplexNo{
- int real, img;
- public:
- //fill in the blacks so that your main works
- ComplexNo(){real=img=0;}
- ComplexNo(int x){
- real=x;
- cout<<"Enter img value: "; cin>>img;
- }
- ComplexNo(int x, int y){real=x; img=y;}
- void showCoplexNo(){
- cout<<real<<(img>=0?"+":"")<<img<<"i"<<endl;
- }
- void add(ComlexNo cn1, ComlexNo cn2, ComlexNo cn3){
- real = cn1.real + cn2.real + cn3.real;
- img = cn1.img + cn2.img + cn3.img;
- }
- ComlexNo add(ComplexNo cn2, ComplexNo cn3){
- ComplexNo temp;
- temp.real = real + cn2.real + cn3.real;
- temp.img = img + cn2.img + cn3.img;
- return temp;
- }
- ComlexNo& operator+(ComplexNo c){
- ComplexNo temp;
- temp.real = real + c.real;
- temp.img = img + c.img;
- return temp;
- }
- };
- int main(){
- ComplexNo c1, c2(10), c3(23,33), c4, c5, c6,c7;
- cout<<"set c1: "<<endl;
- cout<<"c1 before setting = "; c1.showComplexNo();
- c1.setComplexNo();
- c4.add(c1,c2,c3);
- //c5.add(c1,c2,c3,c4);
- c5 = c1.add(c2,c3);
- cout<<"c1 = "; c1.showComplexNo();
- cout<<"c2 = "; c2.showComplexNo();
- cout<<"c3 = "; c3.showComplexNo();
- cout<<"c4 = "; c4.showComplexNo();
- c6 = c1 + c2; cout<<"c6 = "; c7.showComplexNo();
- c7 = c3+c4+c5; cout<<"c7 = "; c7.showComplexNo();
- return 0;
- }
- ------------------
- RUN:
- Enter img value: 12
- set c1:
- c1 before setting = 0+0i
- Enter real value: 4
- Enter img value: -2
- c1 = 4-2i
- c2 = 10+12i
- c3 = 23+33i
- c4 = 37+43i
- c5 = 37+43i
- -----------------------------------
- 1 2 3
- x + y + z
- 3 + 3
- c6 = c1 + c2 + c3 + c4 + c5;
- operator function: special method
- format:
- returnType operator"theOperator"(pType p1,...){
- }
- returnType operator+(pType p1,...){
- }
- c3 = c1+c2; //c3 = c1.operator+(c2);
- c4 = c1+c2+c3; // c1.operator+(c2)
- temp.operator+(c3);
- c4 = c1.operator+(c2).operator+(c3);
- cout<< getSum(x,y) + z;
- --------------------
- c9 = 5 + c2; //operator+(5,c2);
- ---------------
- RT operator=(ClassType c){
- field1 = c.field1;
- field2 = c.field2;
- field3 = c.field3;
- }
- ------------------------------
- class Person{
- int noOfTaskCompleted;
- public:
- Person(){noOfTaskCompleted=0;}
- int getNoOfTaskCompleted(){ return noOfTaskCompleted;}
- //void buySnack(){
- Person buySnack(){
- cout<<"Snacks are bought.. :-)"<<endl;
- noOfTaskCompleted++;
- return *this;
- }
- //void arrangeLivingRomm(){
- Person arrangeLivingRomm(){
- cout<<"Living room is arranged.. :-("<<endl;
- noOfTaskCompleted++;
- return *this;
- }
- void serveSnack(){
- cout<<"Snack is served.. !!!!"<<endl;
- noOfTaskCompleted++;
- }
- };
- //RT callerIsAMethodRepresentingRabbi(){
- int main(){
- Person roudru;
- roudru.buySnack();
- roudru.arrangeLivingRomm();
- roudru.serveSnack();
- //roudru.buySnack().arrangeLivingRoom().serveSnack();
- cout<<"No of task completed is: "
- <<roudru.getNoOfTaskCompleted()<<endl;
- }
- ----------------------
- Student asif;
- Student* ptr = &asif;
- asif.id; //legal
- ptr.id; //illegal
- ptr->id; //legal
- (*ptr).id; //legal
- ==================
- Lab exercise:
- --------------
- class Book{
- //field: isbnNo, title, price;
- //string *autherPtr; int authorNo;
- public:
- //write default constructor [2 marks]
- //add other necessary member functions...
- //declare necessary friend functions...
- };
- //need to overload >> operator to allow chaining
- //Should ask info for all the fields from user
- //need to overload << operator to allow chaining
- //Should display all the fields of the object iff
- //the default values are replaced with user input
- int main(){
- int n, i; cin>>n;
- Book bookArr[n]; //an array of Book objects
- int noOfAuthors = populateBooks(bookArr, n);
- //write this global function
- //This function will replace the default values (set by constructor) of
- //n Books by using cin>>BookObject format in a loop. Finally it returns
- //total number of authors of all books of the array.
- cout<<”No of authors of all books is: “<<noOfAuthors<<endl;
- cout << “Detail of the books are: “ << endl;
- for(i=0; i<n ;i++) cout << bookArr[i] << endl;
- int index = getBookIndexWithLowestPrice(bookArr, n); [2 marks]
- int x; cin>>x;
- if(bookArr[index]<500.0) bookArr[index] += x;
- //bookArr[index] is a book and if it’s
- //price is <500 taka, then += will increase
- //the book’s price by x%. Write necessary
- //operator functions ( < , += )
- Book b1, b2, b3, b4; cin>>b1>>b2>>b3>>b4;
- //overload ‘+’ operator twice.
- //‘+’ means adding price, not books
- cout<<( (b1+b2+b3+b4 > 5000.0)
- ? "Customer is given 10% discount."
- : " customer is given 10% discount."
- ) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement