Advertisement
0xManticore

Student Class

Oct 9th, 2022 (edited)
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. // name: Saleh Mohamed
  2. // matric: A19EC4058
  3. // 09/10/2022
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. class Student {
  10.     private:
  11.         string name;
  12.         int age;
  13.         string matric;
  14.         float CGPA;
  15.    
  16.     public:
  17.         // default constructor
  18.         Student(){
  19.             name = "";
  20.             age = 0;
  21.             matric = "";
  22.             CGPA = 0.0;
  23.         }
  24.        
  25.         // parameterized constructor
  26.         Student(string name, int age, string matric, float CGPA) {
  27.             this->name = name;
  28.             this->age = age;
  29.             this->matric = matric;
  30.             this->CGPA = CGPA;
  31.         }
  32.        
  33.         // accessors
  34.         string getName() { return name; }
  35.         int getAge() { return age;  }
  36.         string getMatric() { return matric; }
  37.         float getCGPA() { return CGPA;  }
  38.        
  39.         // mutators
  40.         void setName(string name){ this->name=name;}
  41.         void setAge(int age){ this->age=age;}
  42.         void setMatric(string matric){ this->matric=matric;}
  43.         void setCGPA(float CGPA) {this->CGPA=CGPA;}
  44.        
  45. };
  46.  
  47.  
  48.  
  49.  
  50. int main() {
  51.     // object using constructor
  52.     Student s1("Saleh Mohamed", 21, "A19EC4058", 1.5);
  53.     // object using methods
  54.     Student s2;
  55.     s2.setName("Ahmed");
  56.     s2.setAge(23);
  57.     s2.setMatric("A22EC0000");
  58.     s2.setCGPA(1.6);
  59.    
  60.     cout << "Student 1" << endl;
  61.     cout << "Name: "<< s1.getName() << endl;
  62.     cout << "Age: "<< s1.getAge() << endl;
  63.     cout << "Matric: "<< s1.getMatric() << endl;
  64.     cout << "CGPA: "<< s1.getCGPA() << endl;
  65.     cout << "-------------------------" << endl;
  66.     cout << "Student 2" << endl;
  67.     cout << "Name: "<< s2.getName() << endl;
  68.     cout << "Age: "<< s2.getAge() << endl;
  69.     cout << "Matric: "<< s2.getMatric() << endl;
  70.     cout << "CGPA: "<< s2.getCGPA() << endl;
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement