Wednesday 6 April 2011

C++ Final Exam Cheat Sheet

So last minute my computer science professor decides to email the class. That we are allowed to bring in a one sided cheat sheet. So seeing as my this is my final exam and is worth 42% of my mark in the class. I decided it might be a good idea to create such a sheet full of info for ease. Last time i heard the exam would only have C++ on it and will deal with mostly classes. So i took a look at all my "class" notes and came up with this cheat sheet/program that goes through everything i learned on classes this semester.

The program i created was rewritten extremely compressed and ugly to save space for future material i decide to put on it. At the moment though here is my cheat sheet.

Source Code
----------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class study
{
public:
    study(){x = 0,y = 0; n++;}
    study(int a, int b);
    void print(void);
    void displayN(void)const;
    friend study change(study obj){ cin>>obj.x; cin>>obj.y; return obj;}
    friend int area(study);
    study& operator+(study);
    study& setX5(void){this->x = 5; return *this;}
    study& sety10(void){this->y = 10; return *this;}
    friend ostream& operator<<(ostream &output, study &obj){output<<"("<<obj.x<<","<<obj.y<<")"<<endl; return output;}//stream overloading friend
    ~study(){x = 0; y=0; if(n!=0){cout<<"Object deconstruction"<<" Objects remaining = "<<n--<<endl;}}
private://methods in here called utility function
    static int n;
    int x;
    int y;
};
int study::n = 0;

void study::print(void){cout<<x<<endl; cout<<y<<endl;}
void study::displayN(void)const{cout<<"The number of objects created is: "<<n<<endl;}
study::study(int a, int b){ n++; x = a; y = b;}

int area(study obj){ return obj.x*obj.y;}

study& study::operator+(study obj2){this->x = this->x + obj2.x; this->y = this->y + obj2.y; return *this;}

int main()
{
    study obj1;//x = 0 , y = 0
    study obj2(2,4);//x = 2 , y = 4
    const study obj3(5,4);
    study *obj4 = new study(5,5);
    obj1.print();//output: 0  endl 0
    obj2.print();//output: 2  endl 4
    cout<<"The area of obj2: "<<area(obj2)<<endl;//object from area function gets deconstructed
    obj1 = change(obj1);//x = 6 , y = 5
    obj1 = obj1.operator+(obj2);
    obj1.print();// 8, 9
    obj1 = obj1 + obj2;
    obj1.print();//10,13
    obj1.displayN();//3
    //obj3.print(); won't work because const objs can only use const methods
    obj1.setX5().sety10();//cascading member list
    obj1.print();
    cout<<obj1<<endl;//use of operator overloading of <<. Method has to be a friend. ostream for output. istream for input.
    delete obj4;
    return 0;
}
--------------------------------------------------------------------------------------------------------------

As you can see it is pretty ugly, but should really benefit me in terms of knowing the syntax of certain programing styles/commands.

20 comments:

  1. good luck on the test! :D

    ReplyDelete
  2. Um, what? I'm trying to teach myself the basics of C++ and I still have no clue what your programs say. Let me get this straight, you made a program to make a cheat sheet for a programming test?

    ReplyDelete
  3. look good to me, it's so helpful when you're allowed a formula sheet.

    ReplyDelete
  4. i only have a basics of C++, but doesnt it acually describe studying? :)

    ReplyDelete
  5. hm nothing special there :3

    ReplyDelete
  6. i would gladly give you something useful here, but i dont understand anything in matters of IT. so i just wish you the best of luck for your exams

    ReplyDelete
  7. :D nice! but why does he let u do this?

    ReplyDelete
  8. I can't understand much of that at all haha.

    ReplyDelete
  9. Programming is tough. I did Java for a while. It's a lot different than C++

    ReplyDelete
  10. Looks fine. I only know the basics of C++ like stringstream and ifstream etc. Still wanna know how to work with classes..

    ReplyDelete
  11. Good luck! One of my teachers at the university also had the same system. At first exam one sided sheet and at second two sided.

    ReplyDelete
  12. Looks like a good cheat sheet. Goodluck on your test man! :) Followed

    ReplyDelete
  13. I might be "borrowing" that cheatsheet some time :P

    ReplyDelete
  14. I agree with most of your points, because this article gives the light in which we can observe the reality and I take positive information on this great blog. It is very effective data provide me as well as and new experience learn from it. We can use this cell phone detector and cell phone detection to stop this cheating over phone.

    ReplyDelete