Syntac'c sugar : Syntax in a programming language that - - PowerPoint PPT Presentation

syntac c sugar syntax in a programming language that
SMART_READER_LITE
LIVE PREVIEW

Syntac'c sugar : Syntax in a programming language that - - PowerPoint PPT Presentation

Syntac'c sugar : Syntax in a programming language that makes something easier to express. Example of syntac9c sugar int x = 1, y = 2; int


slide-1
SLIDE 1
slide-2
SLIDE 2
  • Syntac'c ¡sugar: ¡Syntax ¡in ¡a ¡

programming ¡language ¡ that ¡makes ¡something ¡ easier ¡to ¡express. ¡

slide-3
SLIDE 3

Example ¡of ¡syntac9c ¡sugar ¡

int ¡x ¡= ¡1, ¡y ¡= ¡2; ¡ int ¡z ¡= ¡x ¡+ ¡y; ¡ ¡ int ¡x ¡= ¡1, ¡y ¡= ¡2; ¡ int ¡z ¡= ¡add(x, ¡y) ¡

Many ¡operators ¡are ¡syntac-c ¡sugar ¡because ¡ usually ¡they ¡are ¡unnecessary ¡in ¡the ¡language; ¡ we ¡could ¡get ¡by ¡with ¡just ¡func-ons. ¡

slide-4
SLIDE 4

Example ¡of ¡syntac9c ¡sugar ¡

vector<int> ¡vec(3); ¡ vec[0] ¡= ¡100; ¡ cout ¡<< ¡vec[0]; ¡ ¡ vector<int> ¡vec(3); ¡ vec.set(0, ¡100); ¡ cout ¡<< ¡vec.get(0); ¡

Many ¡operators ¡are ¡syntac-c ¡sugar ¡because ¡ usually ¡they ¡are ¡unnecessary ¡in ¡the ¡language; ¡ we ¡could ¡get ¡by ¡with ¡just ¡func-ons. ¡

slide-5
SLIDE 5

Operator ¡overloading ¡

  • Func'on ¡overloading: ¡Allowing ¡different ¡

func9ons ¡with ¡the ¡same ¡name, ¡dis9nguished ¡ by ¡argument ¡number ¡or ¡data ¡type(s). ¡

  • Operator ¡overloading: ¡Adding ¡new ¡meanings ¡

for ¡operators ¡when ¡used ¡with ¡different ¡data ¡

  • types. ¡
slide-6
SLIDE 6

As ¡simple ¡as ¡defining ¡a ¡func9on ¡

  • Define ¡a ¡func9on ¡called: ¡
  • perator+ ¡ ¡operator-­‑

¡ ¡operator* ¡operator/ ¡

  • perator+= ¡operator<

¡operator++ ¡operator== ¡

  • Number ¡of ¡arguments ¡is ¡determined ¡by ¡the ¡
  • perator ¡name. ¡

– i.e., ¡operator+ ¡always ¡takes ¡two ¡arguments. ¡

  • Return ¡type ¡can ¡be ¡anything ¡you ¡want. ¡
slide-7
SLIDE 7

Overloading ¡+ ¡

vector<int> ¡vec1, ¡vec2, ¡vec3; ¡ vec1.push_back(1); ¡ vec1.push_back(2); ¡ vec2.push_back(10); ¡ vec2.push_back(20); ¡ vec3 ¡= ¡vec1 ¡+ ¡vec2; ¡ ¡

slide-8
SLIDE 8

Overloading ¡+ ¡

vector<int> ¡vec1, ¡vec2, ¡vec3; ¡ vec1.push_back(1); ¡ vec1.push_back(2); ¡ vec2.push_back(10); ¡ vec2.push_back(20); ¡ vec3 ¡= ¡vec1 ¡+ ¡vec2; ¡ cout ¡<< ¡vec3; ¡

slide-9
SLIDE 9

Overload ¡these ¡operators ¡

vector<int> ¡vec, ¡vec2; ¡ vec ¡+= ¡1; ¡ ¡// ¡overload ¡+= ¡so ¡it ¡does ¡push_back ¡ vec ¡+= ¡2; ¡ vec ¡+= ¡1; ¡ vec ¡+= ¡3; ¡ vec2 ¡= ¡vec ¡– ¡1; ¡ ¡ ¡// ¡vec2 ¡is ¡now ¡[2, ¡3] ¡ ¡ ¡ ¡ ¡ ¡// ¡overload ¡minus ¡so ¡it ¡removes ¡all ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡all ¡instances ¡of ¡an ¡item ¡from ¡a ¡vector ¡

slide-10
SLIDE 10

Overloading ¡with ¡classes ¡

class ¡ra9onal ¡{ ¡ ¡ ¡public: ¡… ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡int ¡num, ¡den; ¡ }; ¡ ¡ ra9onal ¡operator* ¡(const ¡ra9onal ¡& ¡a, ¡const ¡ra9onal ¡& ¡b) ¡ { ¡ ¡ ¡ra9onal ¡ans; ¡ ¡ ¡ans.num ¡= ¡a.num ¡* ¡b.num; ¡ ¡ ¡ans.den ¡= ¡a.den ¡* ¡b.den; ¡ ¡ ¡return ¡ans; ¡ } ¡

slide-11
SLIDE 11

Solu9on ¡1 ¡

class ¡ra9onal ¡{ ¡ ¡ ¡public: ¡ ¡ ¡ ¡ ¡ra;onal ¡operator*(const ¡ra;onal ¡& ¡b); ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡int ¡num, ¡den; ¡ }; ¡ ¡ ra;onal ¡ra;onal::operator* ¡(const ¡ra;onal ¡& ¡b) ¡ { ¡ ¡ ¡ra9onal ¡ans; ¡ ¡ ¡ans.num ¡= ¡num ¡* ¡b.num; ¡ ¡ ¡ans.den ¡= ¡den ¡* ¡b.den; ¡ ¡ ¡return ¡ans; ¡ } ¡

slide-12
SLIDE 12

Solu9on ¡2 ¡

class ¡ra9onal ¡{ ¡ ¡ ¡public: ¡ ¡ ¡ ¡ ¡friend ¡ra9onal ¡operator* ¡(const ¡ra9onal ¡& ¡a, ¡const ¡ra9onal ¡& ¡b) ¡ ¡ ¡private: ¡ ¡ ¡ ¡ ¡int ¡num, ¡den; ¡ }; ¡ ¡ ra9onal ¡operator* ¡(const ¡ra9onal ¡& ¡a, ¡const ¡ra9onal ¡& ¡b) ¡ { ¡ ¡ ¡ra9onal ¡ans; ¡ ¡ ¡ans.num ¡= ¡a.num ¡* ¡b.num; ¡ ¡ ¡ans.den ¡= ¡a.den ¡* ¡b.den; ¡ ¡ ¡return ¡ans; ¡ } ¡

slide-13
SLIDE 13

In ¡your ¡ra9onal ¡class ¡

  • Overload ¡<< ¡ ¡(will ¡need ¡to ¡be ¡a ¡friend) ¡
  • Overload ¡+ ¡ ¡

¡(put ¡inside ¡class) ¡

  • Overload ¡< ¡

¡(put ¡inside ¡class) ¡