fork download
  1. #include<bits/stdc++.h>
  2. #define IOF ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  3. #define ll long long
  4. #define pb public:
  5. #define pr private:
  6. using namespace std;
  7. class Complex{
  8. float real , imag;
  9. public:
  10. Complex(){
  11. real = imag = 0;
  12. cout << "This is the default constructor\n";
  13. }
  14. Complex(float f){
  15. real = imag = f;
  16. cout << "This is the overloaded constructor , with 1 parameter\n";
  17. }
  18. Complex(float f1 , float f2){
  19. real = f1;
  20. imag = f2;
  21. cout << "This is the overloaded constructor , with 2 parameters\n";
  22. }
  23. void setReal(float r){
  24. real = r;
  25. }
  26. void setImag(float i){
  27. imag = i;
  28. }
  29. float getReal(){
  30. return real;
  31. }
  32. float getImag(){
  33. return imag;
  34. }
  35. void print(){
  36. cout << real << (imag < 0 ? " - " : " + ") << abs(imag) << 'i' << '\n';
  37. }
  38. Complex add(Complex c){
  39. Complex tmp;
  40. tmp.real = real + c.getReal();
  41. tmp.imag = imag + c.getImag();
  42. return tmp;
  43. }
  44. Complex sub(Complex c){
  45. Complex tmp;
  46. tmp.real = real - c.getReal();
  47. tmp.imag = imag - c.getImag();
  48. return tmp;
  49. }
  50. friend Complex addTo(float v , Complex c);
  51. ~Complex(){
  52. cout << "the object is destroyed\n";
  53. }
  54. Complex operator +(Complex & c){
  55. Complex tmp;
  56. tmp.real = real + c.real;
  57. tmp.imag = imag + c.imag;
  58. return tmp;
  59. }
  60. Complex operator +(float f){
  61. Complex tmp;
  62. tmp.real = real + f;
  63. tmp.imag = imag;
  64. return tmp;
  65. }
  66. Complex operator -(float f){
  67. Complex tmp;
  68. tmp.real = real - f;
  69. tmp.imag = imag;
  70. return tmp;
  71. }
  72. Complex operator -(Complex & c){
  73. Complex tmp;
  74. tmp.real = real - c.real;
  75. tmp.imag = imag - c.imag;
  76. return tmp;
  77. }
  78. friend Complex operator +(float f , Complex & c);
  79. friend Complex operator -(float f , Complex & c);
  80. bool operator == (Complex & c){
  81. return (real == c.real && imag == c.imag);
  82. }
  83. void operator +=(Complex & c){
  84. real += c.real;
  85. imag += c.imag;
  86. }
  87. Complex operator ++(){
  88. real++;
  89. return *this;
  90. } //pre
  91. Complex operator ++(int){
  92. Complex b;
  93. b.real = real , b.imag = imag;
  94. real++;
  95. return b;
  96. } //post
  97. operator float(){
  98. return real;
  99. }
  100. };
  101. Complex operator +(float f , Complex & c){
  102. Complex b;
  103. b.real = c.real + f;
  104. b.imag = c.imag;
  105. return b;
  106. }
  107. Complex operator -(float f , Complex & c){
  108. Complex b;
  109. b.real = f - c.real;
  110. b.imag = c.imag;
  111. return b;
  112. }
  113. Complex addTo(float v , Complex c){
  114. Complex b;
  115. b.real = c.real + v;
  116. return b;
  117. }
  118. class Stack{
  119. int sz;
  120. int *arr;
  121. int top;
  122. static int counter;
  123. public:
  124. Stack(int n = 10){
  125. top = 0;
  126. sz = n;
  127. arr = new int[sz];
  128. cout << "Stack is created\n";
  129. counter++;
  130. }
  131. Stack(const Stack &tmp){
  132. top = tmp.top;
  133. sz = tmp.sz;
  134. arr = new int[sz];
  135. for(int i = 0 ; i < sz ; i++){
  136. arr[i] = tmp.arr[i];
  137. }
  138. counter++;
  139. cout << "is created\n";
  140. }
  141. int pop(){
  142. if(top == 0){
  143. cout << "Empty\n";
  144. return -1;
  145. }
  146. return arr[--top];
  147. }
  148. friend void viewcontent(Stack tmp);
  149.  
  150. void push(int x){
  151. if(top == sz){
  152. cout << "stack is full\n";
  153. return;
  154. }
  155. arr[top++] = x;
  156. }
  157. static int getCnt(){
  158. return counter;
  159. }
  160. Stack& operator =(const Stack& s){
  161. delete []arr;
  162. top = s.top;
  163. sz = s.sz;
  164. arr = new int[sz];
  165. for(int i = 0 ; i < sz ; i++){
  166. arr[i] = s.arr[i];
  167. }
  168. return *this;
  169. }
  170. ~Stack(){
  171. delete[] arr;
  172. counter--;
  173. cout << "destroyed\n";
  174. }
  175. };
  176. int Stack::counter = 0;
  177. void viewcontent(Stack tmp){
  178. int index = tmp.top;
  179. while(index){
  180. cout << tmp.arr[--index] << '\n';
  181. }
  182. }
  183.  
  184.  
  185. int main() {
  186. Complex c1 , c2(5) , c3(2 , 3);
  187. c1.setReal(3);
  188. c1.setImag(4);
  189. cout << c1.getImag() << '\n';
  190. c2.print() ;
  191. c1.print() ;
  192. c1 = c2 + c3;
  193. c1.print();
  194. c1 = c2 +(float)5;
  195. c1.print() ;
  196. c1 = (float)5 + c2;
  197. c1.print() ;
  198. c1 = c2 - c3;
  199. c1.print();
  200. c1 = c2 -(float)5;
  201. c1.print() ;
  202. c1 = (float)5 - c2;
  203. c1.print() ;
  204. c1++;
  205. c1.print();
  206. cout << (float)c1 << '\n';
  207. return 0;
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. //=========================================OOP===================================================//
  221.  
  222. //========================================This===================================================//
  223. /*
  224. class stud{
  225.   int id;
  226.   public:
  227.   stud(){}
  228.   void print(){
  229.   cout << this << '\n' << id << '\n';
  230.   }
  231.   stud(int id){
  232.   id = id; // now because the parameter that i am passing to the constructor has the same name as the attribute
  233.   // it will assign the parameter to itself so to avoid this thing we use "this"
  234.  
  235.   this->id = id; // now the compiler understands that i mean the private member id
  236.   }
  237. };
  238. -->main
  239. // "this" is a pointer that points to the object that called the function
  240.   stud a , b , c(10);
  241.   a.print();
  242.   b.print();
  243.   c.print();
  244. */
  245.  
  246. /*=============================binary operator overloading========================================//
  247. class sum{
  248.   int x , y;
  249.   public:
  250.   sum(int i = 0 , int j = 0){
  251.   x = i , y = j ;
  252.   }
  253.   void getdata(){
  254.   cout << "Enter x\n";
  255.   cin >> x ;
  256.   cout << "Enter y\n";
  257.   cin >> y ;
  258.   }
  259.   void print(){
  260.   cout << x << ' ' << y << '\n';
  261.   }
  262.   // now to add to objects we need to create a member function that do this
  263.   void add(sum c1 , sum c2){
  264.   x = c1.x + c2.x ;
  265.   y = c1.y + c2.y;
  266.  
  267.   }// this is the basic function
  268.   sum add1(sum c2){
  269.   sum c3 ;
  270.   c3.x = x + c2.x ;
  271.   c3.y = y + c2.y;
  272.   return c3 ;
  273.   }// using '='
  274.   //now using operator overloading
  275.   sum operator+(sum c2){// this can be done with every operator
  276.   sum c3 ;
  277.   c3.x = x + c2.x ;
  278.   c3.y = y + c2.y;
  279.   return c3 ;
  280.   }
  281.  
  282.  
  283. };
  284.  
  285.  
  286. --> main
  287.  sum c1 , c2(1 , 2) , c3;
  288.   c1.getdata();
  289.   //c3.add(c1 , c2); // basic
  290.   // now if i want to use the '=' i will create a function that returns an object
  291.   //c3 = c1.add1(c2);
  292.   // using operator overloading
  293.   c3 = c1 + c2;
  294.   // it is the same as this
  295.   c3 = c1.operator+(c2);
  296.   c3.print();
  297.  
  298.  
  299. */
  300.  
  301.  
  302. /*=========================================friend class===================================================//
  303. class square;
  304. class rectangle{
  305.   int w , h ;
  306.   public:
  307.   int area(){
  308.   return w*h ;
  309.   }
  310.   //friend class square; // i cant do this as i have to make it in the square function not here
  311.   void convert(square);// to use the square class i have to make these two classes friends
  312. };
  313.  
  314. class square{
  315.   int side;
  316.   public:
  317.   void s(int x){
  318.   side = x;
  319.   }
  320.   friend class rectangle;
  321. };
  322.  
  323. void rectangle::convert(square s){
  324.   w = h = s.side;
  325. }
  326. --> main
  327.  square sqr ;
  328.   rectangle rect;
  329.   sqr.s(4);
  330.   rect.convert(sqr);
  331.   cout << rect.area();
  332. */
  333.  
  334. /*=========================================friend fucntion===================================================//
  335. class myclass{
  336.   int a , b ;
  337.   public:
  338.   myclass(int i , int j );// this is a member of this class but it is a prototype
  339.  
  340.   friend int sum(myclass ob);// how to make a friend function
  341.   // and it is not a member of this class
  342. };
  343.  
  344. myclass::myclass(int i , int j){// this is a member of the class but defined outside it
  345.  a= i , b = j;
  346. }
  347.  
  348. int sum(myclass ob){
  349.   return ob.a + ob.b;// why i made a friend function to access the private attributes in it
  350. }
  351.  
  352. class A;// class prototyping
  353. class B;
  354.  
  355. // if i want to add two variables but they are not in the same class
  356. class A{// here i access the class
  357.   private:
  358.   int x , y;
  359.   public:
  360.   A(int i , int j ){// constructor to intialize attributes
  361.   x = i , y = j;
  362.   }
  363.   friend int fun(A a , B b);// making fun a friend of class A
  364.  
  365. };
  366.  
  367. class B{
  368.   private:
  369.   int x , y;
  370.   public:
  371.   B(int i , int j ){
  372.   x = i , y = j;
  373.   }
  374.   friend int fun(A a , B b);// same thing here
  375. };
  376. int fun(A a , B b){// now we can access private attributes as this function is a friend of both a and b
  377.   return a.x + b.x ;
  378. }
  379. --> main
  380.  myclass o(10 , 20);
  381.   cout << sum(o);
  382.   A a(5 ,5);
  383.   B b(5 , 5);
  384.   cout << fun(a , b);
  385. */
  386.  
  387. /*=========================================const===================================================//
  388. class Time{
  389. int h , m , s ;
  390. public:
  391.  void print()const // this is how to declare a const function
  392.  {
  393.   cout << h << ' ' << m << ' ' << s << '\n';
  394.  }
  395.  Time(int h1 , int m1 , int s1){
  396.   h = h1 , m = m1 , s = s1;
  397.  }
  398. };
  399. --> main
  400.  const Time noon(12 , 0 , 0);
  401.   noon.print();
  402.   // when i initalize an object of type const then i can reach only the const functions inside it
  403.   // but when i intialize it the normal way then i can access any function inside it
  404. */
  405.  
  406. /*=========================================static===================================================//
  407. void f(){
  408.  
  409.   static int x =0; // when i type static so whenever i call this function the value of x would be updated
  410.   // like if i called this function 3 times first the val of x would be 1 then i call it again the value
  411.   // of x wont be 0 but it will remain 1 and then update it to 2 and so on
  412.   x++;
  413.  
  414.   cout << x << '\n';
  415. }
  416.  
  417. class student{
  418.   static int count ;
  419.   string name ;
  420.   int id ;
  421.   public:
  422.   static void print(){ // static here make me able to call this function in two ways
  423.   // first way is the way we know
  424.   // second one will be shown below
  425.   cout << count << '\n';
  426.   }
  427.   student(){
  428.   count++;
  429.   name = "no name";
  430.   id = 0;
  431.   cout << count << ' ' << name << ' ' << id << '\n';
  432.   print();
  433.  
  434.   }
  435. };
  436. int student::count = 0;// to give initial value to a static member in the class
  437.  
  438. --> main
  439.  
  440. student s1 , s2;
  441.  student s3[3];
  442.  // i can call the print function like this
  443.  s1.print();
  444.  // or like this;
  445.  student::print();
  446. */
  447.  
  448.  
  449. /*=========================================enum===================================================//
  450.  enum days {sat = 1, sun , mon , tue , wend , thur , fri};
  451. // when we print any day from here it will be printed as a number like sat will be printed as 0
  452. // first element starts at 0
  453. // if i wanted the first element to start from 1 then ill put sat = 1
  454. // they are const values cannot be changed and cant be taken from input
  455. --> main
  456.  days m[7] = {sat , sun , mon , tue , wend , thur , fri};
  457.   for(int i = 0 ; i < 7; i++)cout << m[i] << '\n';
  458.   // this is how to deal with the enum
  459. */
  460.  
  461. //=========================arr of objects and pointer to object==================================//
  462. /*
  463. class student{
  464.   string name ;
  465.   int id;
  466.   public:
  467.   student(){ // empty constructor
  468.   cout << "empty\n";
  469.   id = 0 , name = "no name";
  470.   }
  471.   student(string n , int i){
  472.   cout << "para\n";
  473.   name = n , id = i ;
  474.   }
  475.   void print(void){
  476.   cout << name << ' ' << id << '\n';
  477.   }
  478. };
  479. --> main
  480.  int arr[5] = {10 , 23 , 45 , 32 , 11};
  481.   cout << arr << '\n'; // arr is a pointer pointing to the first element in the array
  482.   // notice that it is a constant pointer so that i cant change its value
  483.   //arr++; // this is wrong
  484.   // but if i want to print the next element i can simply say that
  485.   cout << (arr+1)<< '\n';
  486.   int* ptr ;
  487.   ptr = arr ; // now we made ptr points to the first element of the array
  488.   // and then i can access all elements of the array by this pointer
  489.   // and also i can use this
  490.   ptr++ , ptr-- , ptr+= 2 ; // this will make pointer move to the next element
  491.   student a("ahmed" , 123) , b("bate7a" , 412);
  492.   //student arr1[3] = {student("adek" , 421)}; // array of objects
  493.   // now the first object will call the parametarized constructor and others will call the empty one
  494.   student arr1[3] = {a , b};// now we give the array objects so it will only call the constructor
  495.   // when a , b are first initialized and will call the empty constructor for the third object
  496.   // now lets create a pointer of type student to point to the array
  497.   student *p;
  498.   p = arr1;
  499.   for(int i = 0 ; i < 3 ; i++){
  500.   arr1[i].print();
  501.   // to print the arr1 elements using pointer we do this
  502.   //(p+i)->print();
  503.   // or
  504.   p++->print();
  505.   }
  506. */
  507. /*=========================================new_delete===================================================//
  508. class rectangle{
  509.   int* ptrr , *ptrr1 ;
  510.   public:
  511.   rectangle(int ,int);
  512.   ~rectangle();
  513.   int print(){
  514.   return (*ptrr * *ptrr1) ;
  515.   }
  516.  
  517. };
  518. rectangle::rectangle(int a , int b){
  519.  ptrr = new int ;
  520.  ptrr1 = new int;
  521.  *ptrr = a , *ptrr1 = b ;
  522. }
  523.  
  524. rectangle::~rectangle(){
  525.   delete ptrr ;
  526.   delete ptrr1;
  527. }
  528. --> main
  529. void* ptr ; // i can declare a void ptr that can be linked to any data type
  530.   int *ptr1 ;
  531.   ptr1 = new int ;
  532.   // this make the compiler give the poitner a new address to use ;
  533.   *ptr1 = 10 ;
  534.   cout << ptr1 << ' ' << *ptr1 << '\n';
  535.   // now after we finish using the pointer we have to delete it (format it __ pointer actually still exist)
  536.   delete ptr1;
  537.   // now u can reuse the pointer with another address
  538.   ptr1 = new int;
  539.   *ptr1 = 10;
  540.   cout << ptr1 << '\n'; // this prints the location of the pointer
  541.   cout << &ptr1 << '\n'; // this prints the location the compiler gave me for the new int
  542.   cout << *ptr1 << '\n'; // this prints the value
  543.   delete ptr1 ;
  544.  
  545.   // new and delete with class
  546.   rectangle recta(3 , 5) , rectb(4 , 6);
  547.   cout << recta.print() << '\n';
  548.   cout << rectb.print() << '\n';
  549. */
  550.  
  551. /*=========================================struct===================================================//
  552. struct car{
  553.   string name , color ;
  554.   int speed , model;
  555. }g , k;// a way to declare a struct
  556.  
  557. struct exam{
  558.   double first , second , final;
  559.   // i can also write functions in here
  560.   void p(){
  561.   cout << "struct\n";
  562.   }
  563. };
  564.  
  565. class subject{
  566.   private:
  567.   string name ;
  568.   exam Exam;
  569.   public:
  570.   subject(){
  571.   name = "no name";
  572.   Exam = {0 , 0 ,0}; // this can happen only when i give the struct initial value
  573.   }
  574.   subject(string nam1e , double fi , double se , double fin){
  575.   name = nam1e ;
  576.   Exam.final = fin , Exam.second = se , Exam.first = fi;
  577.   Exam.p();
  578.   }
  579.   double tot(){
  580.   return Exam.final + Exam.first + Exam.second;
  581.   }
  582.   void print(){
  583.   cout << tot() << '\n';
  584.   // if i want to print anything else
  585.   }
  586. };
  587. --> main
  588.  g = {"ss" , "sq" , 22 , 23};
  589.   cout << g.speed << '\n'; // thats how to initialize a struct and access element in it
  590.   // how to compare a struct i cant say if(k>g) cant compare the whole struct
  591.   // but i can compare every element with another element
  592.  
  593.   if(g.speed < g.model)cout << 1 << '\n'; // i can also compare to elements from the same struct
  594.   // to deal with functions we can simply say that the struct is like int or long long and can use it as i want in functions
  595.   // how to combine struct with class
  596.   subject e("OOp" , 24 , 34 , 350);
  597.   e.print();
  598. */
  599.  
  600. /*=========================================destructor===================================================//
  601. // the same name as the class
  602. // starts with (~)
  603. // doesnt have params and doesnt retrun anything
  604. // a class has only one destructor
  605. // deletes dynamic object from the memory
  606. // deletes the objects from bottom to top
  607. // will be called when the right brace closes
  608. class rectangle{
  609.   private:
  610.   int W , H ;
  611.   public:
  612.   // if there is param constructor so i have to create the object with params
  613.   // if i dont want to create an object with params then i have to put the empty constructor to avoid errors
  614.   rectangle(int a , int b):W(a) , H(b)// i can set attributes this way
  615.   {
  616.   cout << "A rectangle has been created\n";
  617.   // i can create an object here too
  618.   }
  619.   ~rectangle(){
  620.   cout << W << ' ' << H << '\n';
  621.   cout << "A rectangle has been deleted\n";
  622.   }
  623. };
  624.  
  625. // another example on how we can use functions
  626. class phone{
  627.   // i dont have to use private here
  628.   string name , model;
  629.   int price;
  630.   public:
  631.   phone(string a , string b , int p):name(a) , model(b) , price(p)
  632.   {// i have to put braces here
  633.   cout << "Phone\n";
  634.   }
  635.   void print();// i can type the prototype of the function here and complete it anywhere
  636.   // i can also type a prototype of destructor here
  637.   ~phone();
  638. };
  639. // like here but i must type the class name with scope resolution
  640. phone::~phone(){
  641.   cout << "object has been destroyed\n";
  642. }
  643. void phone::print(){
  644.   cout << name << ' ' << model << ' ' << price << '\n';
  645. }
  646.  
  647. class student{// example
  648.   string name ;
  649.   int id;
  650.   public:
  651.   student(){
  652.   cout << "object created\n";
  653.   }
  654.   student(string s , int a)
  655.   { name = s , id = a ;
  656.   cout << "object created\n";
  657.   }
  658.   void set_name_id(string s , int a ){
  659.   name = s , id = a;
  660.   }
  661.   ~student(){
  662.   cout << "object destroyed\n";
  663.   }
  664.   void print(){
  665.   cout << name << ' ' << id << '\n';
  666.   }
  667. };
  668.  
  669. void f(student& s){
  670.   student s1;
  671.   s1 = s;
  672.   s.set_name_id("sami" , 12345);
  673.   s.print();
  674.   s1.print();
  675. }
  676. --> main
  677.  rectangle R1(3 , 4) , R2(4 , 6);
  678.   phone("sam" , "any" , 15);
  679.   student st1("ahmed" , 1111) , st2("moh" , 22222);
  680.   cout << "going to function\n";
  681.   f(st1);
  682.   cout << "back from function\n";
  683.   st1.print();
  684.  
  685. */
  686.  
  687.  
  688. /*========================================constructor=================================================//
  689. // have the same name as the class
  690. // dont return any value even void
  691. // when an object is created the constructor is called immediately
  692. class triangle{
  693.   private:
  694.   double base , height;
  695.   public:
  696.   // empty constructor
  697.   triangle(){// thats how we define empty constructor it is called when an object is created
  698.   cout << "first constructor\n";
  699.   // we can also use constructor to give initial values to the attributes
  700.   base = 0 , height = 0;
  701.   }
  702.   // parameterized constructor we can use it as a set function to set only initial values
  703.   triangle(int b , int h ){//there is at least one parameter in it
  704.   base = b , height = h;
  705.   cout << "parameterized constructor\n";
  706.   }
  707.   //overloading constructor
  708.   triangle(int b){// this is made to set only the base value
  709.   // so when i dont input the height it will get here not the previous one
  710.   // and if i give the parameter h initial value in the constructor above it will be error
  711.   base =b ;
  712.   }
  713.   void setBase_height(double b , double h){
  714.   base = b , height = h;
  715.   }
  716.   double area(){
  717.   return 0.5*base*height ;
  718.   }
  719.   void print(){
  720.   cout << base << ' ' << height << ' ' << area() << '\n';
  721.   }
  722. };
  723. // copy constructor example
  724. class Copy{// we use copy constructor when we have a lot of attributes
  725.   private:
  726.   int a1 , a2 , a3 ,a4 , a5 , a6 , a7 , a8;
  727.   public:
  728.   int x ; // i can create variables in public and can access them in main
  729.   Copy(int aa1 , int aa2 , int aa3 , int aa4 , int aa5 , int aa6 , int aa7 , int aa8)
  730.   {
  731.   a1 = aa1 , a2 = aa2 , a3 = aa3 , a4 = aa4 , a5 = aa5 , a6 = aa6 , a7 = aa7 , a8 = aa8 ;
  732.   }
  733.   // instead of doing the param constructor we can do a copy constructor by passing a class to it
  734.   Copy(const Copy& a){// to pass a class we have to type it like this
  735.   a1 = a.a1 , a2 = a.a2 , a3 = a.a3 , a4 = a.a4 , a5 = a.a5 , a6 = a.a6 , a7 = a.a7 , a8 = a.a8;
  736.   // here iam able to use the private attributes of the constructor
  737.   // but in main i cant i only can use the public functions
  738.   // we cant use public functions or attributes in here
  739.   }
  740.   void print(){
  741.   cout << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4 << ' ' << a5 << ' ' << a6 << ' ' << a7 << ' ' << a8 << '\n';
  742.   }
  743. };
  744. --> main
  745.  // now if we create an object the constructor will start and print the message inside
  746.   triangle ob; // if i added more objects constructor will start (number of objects)times
  747.   triangle ob1(5 , 10); // now it wont call the empty constructor it will call the parameterized one
  748.   ob1.print();
  749.   //if i wanted to edit the values of the attributes i will use the set function
  750.   ob1.setBase_height(5 , 10);
  751.   //if there is more than one constructor it will only call one of them
  752.   // how to use copy constructor
  753.   Copy g(1 , 2 , 3 , 4 , 5 , 6 , 7 ,8);
  754.   g.print();
  755.   // now to pass an object
  756.   Copy h(g);
  757.   h.print();
  758.   h.x = 5;// how to access public variables in a class
  759.   cout << h.x ;
  760.  
  761. */
  762.  
  763. //========================================class=================================================//
  764. /* functions
  765. class car{// to create a class we type(class , class name)
  766.   private: // here we type the attributes of the class , we cant access this attributes in main
  767.   string name;
  768.   char color[15];
  769.   int maxspeed;
  770.   int model;
  771.   public: // here we type the functions that can access the attributes of the class
  772.   void setName(string n){
  773.   name = n;
  774.   }
  775.   void setColor(char n[]){
  776.   strcpy_s(color , n);
  777.   }
  778.   void setMaxspeed(int m){
  779.   maxspeed = m;
  780.   }
  781.   void setModel(int m ){
  782.   model = m;
  783.   }// these are functions to set the variables
  784.   string getName(){
  785.   return name;
  786.   }
  787.   char* getColor(){// here we typed (*) so that the return type will be array of chars
  788.   return color;
  789.   }
  790.   int getMaxspeed(){
  791.   return maxspeed;
  792.   }
  793.   int getModel(){
  794.   return model;
  795.   }// these are all functions to get the attribute
  796.   void print(){
  797.   cout << name << ' ' << color << ' ' << maxspeed << ' ' << model << '\n';
  798.   }// to print the attributes
  799.  
  800. };
  801. // another example
  802. class triangle{
  803.   private:
  804.   double base , height;
  805.   public:
  806.   void setBase_height(double b , double h){
  807.   base = b , height = h;
  808.   }
  809.   double getBase(){
  810.   return base;
  811.   }
  812.   double getHeight(){
  813.   return height;
  814.   }
  815.   void print(){
  816.   cout << base << ' ' << height << '\n';
  817.   }
  818. };
  819.   using classes in main
  820.   // to access a class we should create an object with the class type
  821.   car x ;// now we created an object with type car which has all the functions in the class
  822.   // to access the functions we type (varName.)then we get all the functions in the class
  823.   // anything typed in the class is called a member --> member private , member function
  824.   x.setColor("black");
  825.   x.setMaxspeed(300);
  826.   x.setModel(2015);
  827.   x.setName("BMW");
  828.   x.print();
  829.   cout << x.getMaxspeed() << '\n';
  830.   triangle xyz ;
  831.   xyz.setBase_height(14.0 , 13.2);
  832. */
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859. // pointers
  860. /* Using poitners to access single values
  861.   int i = 16;
  862.   int* ptr ; // * is for declaring that this var is a pointer
  863.   ptr = &i; // & is for getting the address of i
  864.   cout << ptr << ' ' << *ptr << ' ' << i << '\n'; // printing address , value , * is dereference operator
  865.   *ptr += 5; // if i want to access the value of the address use the derefernce operator
  866.   cout << *ptr << ' ' << i ; // the change happens in i too
  867.   */
  868. //===============================================================================================//
  869. /*
  870.   char msg[] = "Hello"; // making array of chars and notice that the name of the array is a pointer pointing to the first char(address) of the array
  871.   // so now if i said cout << msg << ' ' << *msg ; it will print the whole string and the first address value
  872.   // also the array looks like 'H' , 'e' , 'l' , 'l' , 'o' , '\0'-->means that the array ends here called null
  873.   // the name of the array is constant pointer meaning that i cant point to anywhere else except the array itself
  874.   char* ptr ; // now we declare a pointer char type
  875.   ptr = msg; // why i didnt put & before msg bec we said that msg is actually a pointer
  876.   *ptr = 'M'; // now the first char of the array is changed to M
  877.   cout << *msg << '\n' ; // will print the first char after being changed
  878.   cout << ptr << ' ' << *ptr << '\n' ; // print the whole array from the address of the pointer to the end of the string and value before changing
  879.   ptr++ ; // now when using ++ or -- or += or whatever it will change the address of the pointer
  880.   // by moving it to the next address
  881.   // for example if we say ptr points to 3000(address) so because it is of char type it will be 3001
  882.   // but if it is of int type it will be 3004 so it actually adds up the index*(size of data type) to the address
  883.   *ptr = 'a' ; // now second char equals a
  884.   cout << ptr << ' ' << *ptr ; // printing the string from (ptr to end) and value after changing
  885.   cout << '\n' << &ptr ; // thats if i wanted to print the address of the element iam on now
  886.   */
  887. //===============================================================================================//
  888. /*
  889.   int arr[] = {1 , 2 , 3 , 4 , 5};
  890.   cout << *arr << '\n'; // printing first element in the array
  891.   cout << arr << '\n'; // printing the address of the first element in the array
  892.   // note that it doesnt print the whole array as the array of chars
  893.   for (int i = 0; i < 5; i++)
  894.   {
  895.   cout << *(arr+i) << ' ' ;// here we can access the elements of the array by using the array name as a
  896.   // pointer to the first element of the array and then moving it by the value of i
  897.   }
  898.   for(int i = 0 ; i < 5 ; i++)
  899.   cout << arr+i << ' '; // now we print all the addresses of the array
  900.   */
  901. //===============================================================================================//
  902. /*
  903.   //int* y = &5 ; // this is invalid because iam not giving it an address of a variable
  904.   //swap(5 , 5); // also invalid if passing it be reference or pointer
  905.   int x = 5 , y = 8 ;
  906.   void swap(int& x , int& y){ // passing by reference
  907.   int tmp = x ; // here we just pass the address so that every change applied here well be applied to the variables
  908.   x = y ;
  909.   y = tmp ;
  910.   }
  911.   void swap(int* ptr1 , int* ptr2){ // passing by pointer
  912.   int tmp = *ptr1 ; // here using * is a must because we are using pointers
  913.   *ptr1 = *ptr2 ;
  914.   *ptr2 = tmp ;
  915.   }
  916.   swap(x , y);
  917.   cout << x << ' ' << y << '\n';
  918.   int* ptr1 = &x ;
  919.   int* ptr2 = &y ;
  920.   swap(ptr1 , ptr2);// we can use it like this or --> swap(&x , &y); as we are passing addresses
  921.   cout << *ptr1 << ' ' << *ptr2 ;
  922.   */
  923. //===============================================================================================//
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
This is the default constructor
This is the overloaded constructor , with 1 parameter
This is the overloaded constructor , with 2 parameters
4
5 + 5i
3 + 4i
This is the default constructor
the object is destroyed
7 + 8i
This is the default constructor
the object is destroyed
10 + 5i
This is the default constructor
the object is destroyed
10 + 5i
This is the default constructor
the object is destroyed
3 + 2i
This is the default constructor
the object is destroyed
0 + 5i
This is the default constructor
the object is destroyed
0 + 5i
This is the default constructor
the object is destroyed
1 + 5i
1
the object is destroyed
the object is destroyed
the object is destroyed