Cpp Programming Beginner Exam Focused Codes

-- Leave a Comment
<Pokhara University CPP>
Functions and all types.
Constructor and Destructor
Friend Function
Operation Overloading
Type Conversion (All 3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
using namespace std;
const double pi = 3.14; //If we try to alter the vaule of Pi compiler will generate a error.
class Test2;
class Test{
  int num1; //This will be in private section
  int num2; //This will be in private section
  public:
    //**************************Functions***************************//
    void add3(int a, int b, int c=5){ //Default arguments If only two args are pass last will take 5 if  all 3 are passed it will add those 3 numbers
      int add = a+b+c;
      cout<<"Sum = "<<add<<endl;
    }
    void add3(double a, double b, double c){ //Funtion Overloaded This function and above function name are same but if we pass int as arguments the above will be executed if float are passed this one will be executed
      double add = a+b+c;
      cout<<"Sum = "<<add<<endl;
    }
    void ifunction(){
      cout<<"I am a inline function"<<endl;
    } // FUnction definition inside class will be a inline function
    void anotherIFunc();// this is not a inline function as it is not defined here
    //**************************Constructor Destructor***************************//
    Test(){ //I am a default constructor
      num1=0;
      num2=0;
    }
    Test(int a, int b){ //I am a parameterised constructor
      num1=a;
      num2=b;
    }
    Test(Test &a){ //I am a copy constructor as i coppy the value of passed object to new object
      num1=a.num1;
      num2=a.num2;
    }
    //Here we use A function name Test to preform different taks, which is overload hence a constructor can be Overloaded
    ~Test(){ // Will be called automaticly Cannot be Overloaded as they takes no arguments
      cout<<"I am a Destructor"<<endl;
    }
    //**************************Friend Function***************************//
    friend void iaccess(Test a){//This is a friend function as there is keyword friend
      cout<<a.num2<<endl; // this can access the mamber data at private with a object.
      //no object is required to call a friend function.
    }
    friend void friendoftwo(Test a, Test2 b);
    //**************************Operator Overloading***************************//
    void operator -(){ //TO change the sign so a unary operator As this is unary operator we dont need to pass any args, as we will get a oject when it call
      num1 = num1*(-1);
      num2 = num2*(-1);
    }
    Test operator -(Test a){// TO subtract
      Test tmp;
      tmp.num1 = num1 - a.num1;
      tmp.num2 = num2 - a.num2;
      return tmp;
    }
    void display(){
      cout<<num1<<" and "<<num2<<endl;
    }
    //**************************Type Conversion***************************//
    Test(int a){ //Basic Type to Object Type Conversion
      num1=a;
      num2=a;
    }
    operator int (){ // Object to Basic Type conversion
      return num1+num2;
    }
    /* ******* DO YOU UNDERSTAND ******/ //YOU can OBtain the Object to Object Conversion by Defining constructor in Destination class or Operator conversion function in Source Class.
};
//*****************************************************//
class Test2{
  int age; //This will be in private section
  public:
    Test2(){
      age = 0;
    }
    friend void friendoftwo(Test a, Test2 b);
};
//*****************************************************//

inline void Test :: anotherIFunc(){ //Keyword "inline" makes it a inline function.
  cout<<"I am also a inline function"<<endl;
}

void friendoftwo(Test a, Test2 b){ //We dont need the friend keyword when defining the function.
  int tmp;
  cout<<a.num1<<" and "<<b.age<<endl;
  tmp = a.num1;
  a.num1= b.age;
  b.age = tmp; //Here we swap the private data of two classes using a friend function.
  cout<<a.num1<<" and "<<b.age<<endl;
}
int main()
{
  //Constructor and Function
  Test anum(2,10);
  anum.ifunction();
  anum.anotherIFunc();
  {
    //Destructor
    Test bnum;
    //Distructor will be automaticlly called when the program goes out of this "}"
  }
  //Friend Function
  iaccess(anum);
  Test2 bnum;//bnum.age is 0 because of the default constructor
  friendoftwo(anum, bnum);
  //Operator Overloading
  Test cnum(1,2);
  -cnum;
  cnum.display();
  Test add, dnum(7,15);
  add = dnum - anum;// Here the - FUnction is called by dnum and anum is passed as argument, as num1 is of dnum and a.num1 is of anum UNDERSTND THIS :) BE suer while using Greater then Operator.
  add.display();
  // Type conversion
  Test eanum;
  eanum = 9; //Both num1 and num2 gets the value of 9.
  eanum.display();
  int f = eanum; //f will get the value of num1+ num2 of eanum;
  cout<<f<<endl;

  return 0;
}

Is there anything Please do Comment, We will Update our Post Accordingly.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.

0 comments:

Post a Comment