Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // name: Saleh Mohamed
- // matric: A19EC4058
- // 09/10/2022
- #include <iostream>
- using namespace std;
- class Student {
- private:
- string name;
- int age;
- string matric;
- float CGPA;
- public:
- // default constructor
- Student(){
- name = "";
- age = 0;
- matric = "";
- CGPA = 0.0;
- }
- // parameterized constructor
- Student(string name, int age, string matric, float CGPA) {
- this->name = name;
- this->age = age;
- this->matric = matric;
- this->CGPA = CGPA;
- }
- // accessors
- string getName() { return name; }
- int getAge() { return age; }
- string getMatric() { return matric; }
- float getCGPA() { return CGPA; }
- // mutators
- void setName(string name){ this->name=name;}
- void setAge(int age){ this->age=age;}
- void setMatric(string matric){ this->matric=matric;}
- void setCGPA(float CGPA) {this->CGPA=CGPA;}
- };
- int main() {
- // object using constructor
- Student s1("Saleh Mohamed", 21, "A19EC4058", 1.5);
- // object using methods
- Student s2;
- s2.setName("Ahmed");
- s2.setAge(23);
- s2.setMatric("A22EC0000");
- s2.setCGPA(1.6);
- cout << "Student 1" << endl;
- cout << "Name: "<< s1.getName() << endl;
- cout << "Age: "<< s1.getAge() << endl;
- cout << "Matric: "<< s1.getMatric() << endl;
- cout << "CGPA: "<< s1.getCGPA() << endl;
- cout << "-------------------------" << endl;
- cout << "Student 2" << endl;
- cout << "Name: "<< s2.getName() << endl;
- cout << "Age: "<< s2.getAge() << endl;
- cout << "Matric: "<< s2.getMatric() << endl;
- cout << "CGPA: "<< s2.getCGPA() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement