Lab 4 Oop
Lab 4 Oop
Task 1:
Create a class student with data member ID, NAME, and CGPA. Student class is comprised of
accessor and mutator of its data members. Note ID should be greater than 0 and CGPA should be
in between 0.0 and 4.0. Take input from the user and print it in main
ANSWER:
#include<iostream>
#include<string>
using namespace std;
class student{
int ID;
string NAME;
float CGPA ;
public :
void set_id(int id ){
ID=id;
}
int getid(){
return ID;
}
string getname(){
return NAME;
}
float getcgpa(){
};
int main(){
student jasim ;
int id; string name ; float cgpa;
cout << "ENTER STUDENT ID : ";
cin>>id;
if (id <= 0 )
while (id <= 0){
cout<<" ENTER RIGHT ID : ";
cin>>id;
}
jasim.set_id(id);
cout << "ENTER STUDENT NAME : ";
cin >> name;
jasim.set_name(name);
cout<<"ENTER STUDENT CGPA : ";
cin>>cgpa;
if (cgpa < 0 || cgpa > 4 )
while (cgpa < 0 || cgpa > 4 ){
cout<<"ENTER RIGHT CGPA : ";
cin>>cgpa;
}
jasim.set_cgpa(cgpa);
cout<<"\n\nSTUDENT ID : "<<jasim.getid()<<"\nSTUDENT NAME : "<< jasim.getname()
<<"\nSTUDENT CGPA : "<<jasim.getcgpa()<<endl;
return 0;
}
Task 2:
Re-use the above implemented code and add a default constructor to initialize the class members
to default values. Add a parameterized constructor that initializes the class members to user
defined values. Also add another overloaded constructor that initializes only Name and ID of the
student.
ANSWER:
#include<iostream>
#include<string>
using namespace std;
class student{
int ID;
string NAME;
float CGPA ;
public :
student(){
int ID=0;
string NAME;
float CGPA=0 ;
}
student(int id , string name ,int cgpa ){
float getcgpa(){
return CGPA;
}
};
void main(){
student jasim ;
int id; string name ; float cgpa;
cout << "ENTER STUDENT ID : ";
cin>>id;
if (id <= 0 )
while (id <= 0){
cout<<"ENTER RIGHT ID : ";
cin>>id;
}
cout << "ENTER STUDENT NAME : ";
cin >> name;
cout<<"ENTER STUDENT CGPA : ";
cin>>cgpa;
if (cgpa < 0 || cgpa > 4 )
while (cgpa < 0 || cgpa > 4 ){
cout<<"ENTER RIGHT CGPA : ";
cin>>cgpa;
}
student obj2(id,name,cgpa);
student obj3(id,name);
cout<<"\n\nSTUDENT ID : "<<obj2.getid()<<"\nSTUDENT NAME : "<< obj2.getname()
<<"\nSTUDENT CGPA : "<<obj2.getcgpa()<<endl;
system("pause");
}
Task 3:
Now, again re-use that above implemented code to create a new copy constructor and in the end
add a destructor to the class student. Try to complete all the working, use the copy constructor to
initialize an object and print its data member values using accessor methods.
ANSWER:
#include<iostream>
#include<string>
class student{
int ID;
string NAME;
float CGPA ;
public :
student(){
int ID=0;
string NAME;
float CGPA=0 ;
}
~student (){
cout<<" :: NOW WE ARE IN DESTRUCTOR :: "<<endl;
}
int getid(){
return ID;
}
string getname(){
return NAME;
}
float getcgpa(){
return CGPA;
}
};
int main(){
student jasim ;
int id; string name ; float cgpa;
cout << "ENTER STUDENT ID : ";
cin>>id;
if (id <= 0 )
while (id <= 0){
cout<<"ENTER RIGHT ID : ";
cin>>id;
Task 4:
Add another Pointer Variable to the class Student and initialize this variable with parameterized
constructor. And finally add a timestamp when an object is created and destroyed.
ANSWER:
#include<iostream>
#include<string>
using namespace std;
class student{
int ID;
string NAME;
float CGPA ;
public :
student(){
int ID=0;
string NAME;
float CGPA=0 ;
}
~student (){
cout<<" :: NOW WE ARE IN DESTRUCTOR :: "<<endl;
}
int getid(){
return ID;
}
string getname(){
return NAME;
}
float getcgpa(){
return CGPA;
}
};
int main(){
student jasim ;
int id; string name ; float cgpa;
cout << "ENTER STUDENT ID : ";
cin>>id;
if (id <= 0 )
while (id <= 0){
cout<<"ENTER RIGHT ID : ";
cin>>id;
}
cout << "ENTER STUDENT NAME : ";
cin >> name;
cout<<"ENTER STUDENT CGPA : ";
cin>>cgpa;
if (cgpa < 0 || cgpa > 4 )
while (cgpa < 0 || cgpa > 4 ){
cout<<"ENTER RIGHT CGPA : ";
cin>>cgpa;
}
student *obj2;
obj2 = new student (id,name,cgpa); // parameteric constructor
cout<<"\n\nSTUDENT ID : "<<obj2->getid()<<"\nSTUDENT NAME : "<< obj2->getname()
<<"\nSTUDENT CGPA : "<<obj2->getcgpa()<<endl; //Print values of paramertric constructor
return 0;
}
Total 40 Signature