1 / 18

//: C09:MacroSideEffects.cpp #include "../require.h" #include <fstream> using namespace std;

//: C09:MacroSideEffects.cpp #include "../require.h" #include <fstream> using namespace std; #define BAND(x) (((x)>5 && (x)<10) ? (x) : 0) int main() { ofstream out("macro.out"); assure(out, "macro.out"); for(int i = 4; i < 11; i++) { int a = i; out << "a = " << a << endl << 't';

ursa
Download Presentation

//: C09:MacroSideEffects.cpp #include "../require.h" #include <fstream> using namespace std;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. //: C09:MacroSideEffects.cpp #include "../require.h" #include <fstream> using namespace std; #define BAND(x) (((x)>5 && (x)<10) ? (x) : 0) int main() { ofstream out("macro.out"); assure(out, "macro.out"); for(int i = 4; i < 11; i++) { int a = i; out << "a = " << a << endl << '\t'; out << "BAND(++a)=" << BAND(++a) << endl; out << "\t a = " << a << endl; } } ///:~

  2. a = 4 BAND(++a)=0 a = 5 a = 5 BAND(++a)=8 a = 8 a = 6 BAND(++a)=9 a = 9 a = 7 BAND(++a)=10 a = 10 a = 8 BAND(++a)=0 a = 10 a = 9 BAND(++a)=0 a = 11 a = 10 BAND(++a)=0 a = 12

  3. //: C09:Inline.cpp // Inlines klasis SigniT #include <iostream> #include <string> using namespace std; class Point { int i, j, k; public: Point(): i(0), j(0), k(0) {} Point(int ii, int jj, int kk) : i(ii), j(jj), k(kk) {}

  4. void print(const string& msg = "") const { if(msg.size() != 0) cout << msg << endl; cout << "i = " << i << ", " << "j = " << j << ", " << "k = " << k << endl; } }; int main() { Point p, q(1,2,3); p.print("value of p"); q.print("value of q"); } ///:~

  5. //: C09:Access.cpp // Inline wvdomiTi //funqciebi class Access { int i; public: int read() const { return i; } void set(int ii) { i = ii; } }; int main() { Access A; A.set(100); int x = A.read(); } ///:~

  6. // Accessors & mutators class Rectangle { int wide, high; public: Rectangle(int w = 0, int h = 0) : wide(w), high(h) {} int width() const { return wide; } // Read void width(int w) { wide = w; } // Set int height() const { return high; } // Read void height(int h) { high = h; } // Set }; int main() { Rectangle r(19, 47); // siganisa da simaRlis Secvla r.height(2 * r.width()); r.width(2 * r.height()); } ///:~

  7. //: C09:Rectangle2.cpp // Accessors & mutators with "get" //and "set" class Rectangle { int width, height; public: Rectangle(int w = 0, int h = 0) : width(w), height(h) {} int getWidth() const { return width; } void setWidth(int w) { width = w; } int getHeight() const { return height; } void setHeight(int h) { height = h; } };

  8. int main() { Rectangle r(19, 47); // SecvaleT sigane da //simaRle: r.setHeight(2 * r.getWidth()); r.setWidth(2 * r.getHeight()); } ///:~

  9. //: C09:Stack4.h // With inlines #ifndef STACK4_H #define STACK4_H #include "../require.h" class Stack { struct Link { void* data; Link* next; Link(void* dat, Link* nxt): data(dat), next(nxt) {} }* head;

  10. public: Stack() : head(0) {} ~Stack() { require(head == 0, "Stack not empty"); } void push(void* dat) { head = new Link(dat, head); }

  11. void* peek() const { return head ? head->data : 0; } void* pop() { if(head == 0) return 0; void* result = head->data; Link* oldHead = head; head = head->next; delete oldHead; return result; } }; #endif // STACK4_H ///:~

  12. //: C09:Stack4Test.cpp //{T} Stack4Test.cpp #include "Stack4.h" #include "../require.h" #include <fstream> #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { requireArgs(argc, 1); // File name is // argument ifstream in(argv[1]); assure(in, argv[1]); Stack textlines; string line;

  13. // failis wakiTxva da //striqonebis Senaxva //stekSi while(getline(in, line)) textlines.push(new string(line)); // striqonebis amogdeba //stekidan da dabeWdva: string* s; while((s = (string*)textlines.pop()) != 0) { cout << *s << endl; delete s; } } ///:~

  14. //: C09:EvaluationOrder.cpp // Inline evaluation order class Forward { int i; public: Forward() : i(0) {} // gamoucxadebeli funqciis // gamoZaxeba: int f() const { return g() + 1; } int g() const { return i; } }; int main() { Forward frwd; frwd.f(); } ///:~

  15. // Hidden activities in inlines #include <iostream> using namespace std; class Member { int i, j, k; public: Member(int x = 0) : i(x), j(x), k(x) {} ~Member() { cout << "~Member" << endl; } };

  16. class WithMembers { Member q, r, s; // Have // constructors int i; public: WithMembers(int ii) : i(ii) {} // Trivial? ~WithMembers() { cout << "~WithMembers" << endl; } }; int main() { WithMembers wm(1); } ///:~

  17. //: C09:Noinsitu.cpp // Removing in situ functions class Rectangle { int width, height; public: Rectangle(int w = 0, int h = 0); int getWidth() const; void setWidth(int w); int getHeight() const; void setHeight(int h); }; inline Rectangle::Rectangle(int w, int h) : width(w), height(h) {} inline int Rectangle::getWidth() const { return width; }

  18. 17-18 leqcia inline funqciebi klasSi inline void Rectangle::setWidth(int w) { width = w; } inline int Rectangle::getHeight() const { return height; } inline void Rectangle::setHeight(int h) { height = h; } int main() { Rectangle r(19, 47); // Transpose width & height: int iHeight = r.getHeight(); r.setHeight(r.getWidth()); r.setWidth(iHeight); } ///:~

More Related