Object Oriented Programming (Code)

Introduction: Hello and welcome to a tutorial on Object Oriented Programming, with code examples as opposed to theory. Ready the theory page Before starting this page make sure you have read and understand the theory page which can be found here. Instances The use of OOP is to have multiple instances of one class running at the same time, each with it's own variables and methods. For the tutorials below I am going to use an example of a worker/student database where user information gets entered to a database. Examples: So, first we need to create a class where we want to run the instances of our OOP from as well as the OOP class which will contain the variables and methods of our OOP variable types... Main class:
  1. public class Main {
  2. public static void main(String args[]) {
  3. //This is ran first out of everything. It's also commented out by the front two "//" so this line doesn't get ran.
  4. }
  5. }
OOP class:
  1. public class Student {
  2. private String firstName;
  3. private String lastName;
  4. private int age;
  5. public Student(String fName, String lName, int studentAge) {
  6. this.firstName = fName;
  7. this.lastName = lName;
  8. this.age = studentAge;
  9. }
  10. }
So now we are going to create a few user variables using our "Student" OOP class. In our constructor for our "Student" class we accept the arguments; First Name, Last Name and Age, so we must write these when creating our new variables...
  1. public class Main {
  2. public static void main(String args[]) {
  3. //This is ran first out of everything. It's also commented out by the front two "//" so this line doesn't get ran.
  4. Student first = new Student("John", "Qwop", 34);
  5. Student second = new Student("Sam", "Sail", 16);
  6. Student third = new Student("Lisa", "Porter", 23);
  7. }
  8. }
Now we have created some variables through OOP. We can now use the methods created in our OOP ("Student") class while keeping them separate. To show this, here is a custom method in the "Student" class which output the information to the console of the student:
  1. public void outputDetails() {
  2. System.out.println("This student is " + this.firstName + " " + this.lastName + " and is " + this.age + " years of age.");
  3. }
Here is the use of that method from our Main class on all created students...
  1. first.outputDetails();
  2. third.outputDetails();
And here is the output in the console once the program has finished running...
This student is John Qwop and is 34 years of age.
This student is Lisa Porter and is 23 years of age.
Finished!

Comments

Submitted byAnonymous (not verified)on Fri, 08/21/2020 - 13:37

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Bank
  6. {
  7. private:
  8. char name[30];
  9. char accname[20];
  10. char acctype[20];
  11. float ibalance;
  12. float depos;
  13. float with;
  14. int pass;
  15.  
  16. public:
  17. void menu(void);
  18. void getdata(void);
  19. void deposite(void);
  20. void checkbal(void);
  21. void withdraw(void);
  22. void info(void);
  23. };
  24. void Bank::menu()
  25. {
  26. int num;
  27. cout<<"\n\n\n\t********************************";
  28. cout<<"\n Choose your Option:(1-to-6)\n"<<endl;
  29. cout<<" 1.Create An Account\n";
  30. cout<<" 2.Deposite the Balance\n";
  31. cout<<" 3.Check the Balance\n";
  32. cout<<" 4.Withdraw the Balance\n";
  33. cout<<" 5.See All Information\n";
  34. cout<<" 6.Quit";
  35. cout<<"\n\t********************************";
  36. cout<<"\t\n\t";
  37. cin>>num;
  38. switch(num)
  39. {
  40. case 1:
  41. getdata();
  42. break;
  43.  
  44. case 2:
  45. deposite();
  46. break;
  47.  
  48. case 3:
  49. checkbal();
  50. break;
  51.  
  52. case 4:
  53. withdraw();
  54. break;
  55.  
  56. case 5:
  57. info();
  58. break;
  59.  
  60. default:
  61. break;
  62. }
  63.  
  64. }
  65.  
  66. void Bank::getdata()
  67. {
  68. int go;
  69. cout<<"Enter the Name of Depositer:";
  70. cin>>name;
  71. cout<<"Choose The Account Name:";
  72. cin>>accname;
  73. cout<<"Which types of Account You wanna Create?";
  74. cout<<"\t\t\t\t*Saving\n";
  75. cout<<"\t\t\t\t*\n";
  76. cout<<"\t\t\t\t*\n";
  77. cout<<"\t\n\t";
  78. cin>>acctype;
  79. cout<<"Deposite the Initial Balance:";
  80. cin>>ibalance;
  81. cout<<"Choose your password[0-9] for secure (only 4 nos.):";
  82. cin>>pass;
  83. cout<<"Account Has Been Successfully Created!!!";
  84. cout<<"your account number is:HBL50000"<<pass;
  85. cout<<" Go to MainMenu:";
  86. cin>>go;
  87. if(go==0)
  88. {
  89. menu();
  90. }
  91. }
  92.  
  93. void Bank::deposite()
  94. {
  95. int go;
  96. cout<<"How Much Money You wanna Deposite:";
  97. cin>>depos;
  98. depos+=ibalance;
  99. cout<<"Your Balance Has Been Successfully Deposite!!!\n\n";
  100.  
  101. cout<<"for Go to MainMenu:";
  102. cin>>go;
  103. if(go==0)
  104. {
  105. menu();
  106. }
  107. }
  108.  
  109. void Bank::checkbal()
  110. {
  111. int passcode;
  112. cout<<"Enter your Account's Password: ";
  113. cin>>passcode;
  114. if(pass==passcode)
  115. {
  116. int go;
  117. cout<<"Your Current Balance:"<<" Rs."<<depos<<"\n";
  118. cout<<" for Go to MainMenu:";
  119. cin>>go;
  120. if(go==0)
  121. {
  122. menu();
  123. }
  124. }
  125. else
  126. cout<<"Sorry You Entered Wrong Password!!TRY AGAIN@";
  127. }
  128.  
  129. void Bank::withdraw()
  130. {
  131. int passcode;
  132. cout<<"Enter your Account's Password: ";
  133. cin>>passcode;
  134. if(pass==passcode)
  135. {
  136. int go;
  137. cout<<"How Much Money You wanna Withdraw:";
  138. cin>>with;
  139. depos-=with;
  140. cout<<"After Withdraw Your Balance:"<<" Rs."<<depos<<"\n";
  141. cout<<"for Go to MainMenu:";
  142. cin>>go;
  143. if(go==0)
  144. {
  145. menu();
  146. }
  147. }
  148. else
  149. cout<<"Sorry You Entered Wrong Password!!TRY AGAIN@";
  150. }
  151.  
  152. void Bank::info()
  153. {
  154. int passcode;
  155. cout<<"Enter your Account's Password: ";
  156. cin>>passcode;
  157. if(pass==passcode)
  158. {
  159. int go;
  160. cout<<"All Information is Listed Below:\n";
  161. cout<<"Name of the Depositer:"<<name<<"\n";
  162. cout<<"Name of the Account:"<<accname<<"\n";
  163. cout<<"Type of the Account:"<<acctype<<"\n";
  164. cout<<"Your Current Amount:"<<depos<<"\n";
  165. cout<<" for Go to MainMenu:";
  166. cin>>go;
  167. if(go==0)
  168. {
  169. menu();
  170. }
  171. }
  172. else
  173. cout<<"Sorry You Entered Wrong Password!!TRY AGAIN@";
  174. }
  175.  
  176.  
  177. int main()
  178. {
  179. Bank Obj,ob1;
  180. Obj.menu();
  181. ob1.menu();
  182.  
  183. return 0;
  184. }

Add new comment