0% found this document useful (0 votes)
92 views7 pages

Lab 4 Oop

The document outlines 4 tasks for an Object Oriented Programming lab, requiring a student to create a Student class with data members and methods, add different constructors and a destructor, use a copy constructor, and initialize a pointer variable in the class. The tasks were completed by the student as evidenced by the provided code solutions. A grading sheet at the bottom indicates the tasks were completed but does not show the marks obtained.

Uploaded by

HA Aim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views7 pages

Lab 4 Oop

The document outlines 4 tasks for an Object Oriented Programming lab, requiring a student to create a Student class with data members and methods, add different constructors and a destructor, use a copy constructor, and initialize a pointer variable in the class. The tasks were completed by the student as evidenced by the provided code solutions. A grading sheet at the bottom indicates the tasks were completed but does not show the marks obtained.

Uploaded by

HA Aim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 02
(Spring 2018)

Course: Object Oriented Programming Lab Date:


Course Code: CSL - 210 Max Marks: 40
Faculty’s Name: Aasim Ali Lab Engineer: Shoaib Khan

Name: Mohammad Jasim Waqar Enroll No: 03-135172-055

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;
}

void set_name(string name)


{NAME=name;
}

void set_cgpa(float cgpa){


CGPA=cgpa;
}

int getid(){
return ID;
}

string getname(){
return NAME;
}

float getcgpa(){

Department of Computer Science, BULC


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;
}
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 ){

Department of Computer Science, BULC


ID=id;
NAME=name;
CGPA = cgpa;
}
student (int id , string name ){
ID=id;
NAME=name;
}
int getid(){
return ID;
}
string getname(){
return NAME;
}

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>

Department of Computer Science, BULC


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 ){


ID=id;
NAME=name;
CGPA = cgpa;
}

student (int id , string name ){


ID=id;
NAME=name;
}

student (student & obj2){


ID= obj2.ID;
NAME=obj2.NAME;
CGPA=obj2.CGPA;
}

~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;

Department of Computer Science, BULC


}
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);
student obj4(obj2);
cout<<"\n\nSTUDENT ID : "<<obj4.getid()<<"\nSTUDENT NAME : "<< obj4.getname()
<<"\nSTUDENT CGPA : "<<obj4.getcgpa()<<endl;
return 0;
}

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(int id , string name ,int cgpa ){


ID=id;
NAME=name;
CGPA = cgpa;
}

student (int id , string name ){


ID=id;
NAME=name;
}

student (student & obj2){


ID= obj2.ID;
NAME=obj2.NAME;

Department of Computer Science, BULC


CGPA=obj2.CGPA;
}

~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;
}

Lab Grading Sheet :


Max Obtained
Task Comments(if any)
Marks Marks
1. 10
2. 10
3. 10
4. 10

Total 40 Signature

Department of Computer Science, BULC


Note : Attempt all tasks and get them checked by your Lab Instructor.

Department of Computer Science, BULC

You might also like