0% found this document useful (0 votes)
136 views6 pages

Hamdard University Islamabad Campus: Lab No

The document describes a lab assignment to create a Student class in C++. The Student class holds a student's name, age, and an array of quiz scores. The class includes a default constructor to initialize the attributes, setter and getter methods to modify and retrieve attribute values, and a print method. The main program creates two Student objects using the default constructor and copy constructor, prints their attributes, modifies one object, and prints the attributes again. Adding a custom copy constructor prevents the copied object's attributes from being modified along with the original.

Uploaded by

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

Hamdard University Islamabad Campus: Lab No

The document describes a lab assignment to create a Student class in C++. The Student class holds a student's name, age, and an array of quiz scores. The class includes a default constructor to initialize the attributes, setter and getter methods to modify and retrieve attribute values, and a print method. The main program creates two Student objects using the default constructor and copy constructor, prints their attributes, modifies one object, and prints the attributes again. Adding a custom copy constructor prevents the copied object's attributes from being modified along with the original.

Uploaded by

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

Page 1 of 6

Hamdard University Islamabad Campus


Hamdard Institute of Engineering & Technology
Department of Computing

Program: BS Computer Science

Course Number CS122


Course Title Object Oriented Programming
Semester/Year Fall 2020

Instructor Ms. Saadia Mooqaddas

LAB NO. 04

Assignment Title Lab NO 4

Submission Date
Due Date

Student Name Muhammad Umer Farooq


Student CMS-ID 445-2020
Signature*

*By signing above, you attest that you have contributed to this submission and
confirm that all work you have contributed to this submission is your own work. Any
suspicion of copying or plagiarism in this work will result in an investigation of
Academic Misconduct and may result in a “0” on the work, an “F” in the course, or
possibly more severe penalties.
Page 2 of 6

Lab Tasks
Task # 1
Program Statement:
Write a Student class in C++
A student object should hold information about a student in your grade book and should have the
following details:
o string name
o int age
o int *p
You are required to do the following
Write a default constructor that initializes name, age and p pointer dynamically points to array
of size 3. Add marks in the p array for 3 quizzes.
Create object s1.
Create object s2 exactly similar to s1. (Use default copy constructor supplied by the compiler)
Print the values of s1 and s2 along with address of pointer p
Now change name and marks for s1 using setter function
Print the values of s1 and s2 along with address of pointer p (calling the same print function again)
Change your code and write a copy constructor for this class. Re-run your code

Program Code:
// ConsoleApplication7.cpp : Defines the entry point for the console application.
//Default constructor

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string name;
int *p;
int age;
public:
Student()
{
name = "Umer";
p = new int[3];
age = 19;
int x, y;
for (x = 1; x<=3; x++)
{
cout << "Values of quizzes = ";
cin >> y;
if (y<0)
{
y = 0;
}
else
{
p[x] = y;
Page 3 of 6

}
}

void setter_function()
{
string n;
cout << "Enter name : ";
cin >> n;
name = n;
int x,y;
for (x= 1; x<=3; x++)
{

cout << "Quiz marks= ";


cin >> y;
if (y<0)
{
y = 0;
}
p[x] = y;
}
}
string get_name()
{
return name;

}
int get_age()
{
return age;
}
int* get_pointer()
{
return p;
}
void print()
{
cout << "Name :" << get_name() << endl;
cout << "Age :" << get_age() << endl;
cout << "Pointer address is :" << get_pointer() << endl;
}

};

int main()
{
string pn;
Student s1;
Student s2 = s1;
cout << "Object s1: " << endl;
s1.print();
cout << "Object s2" << endl;
Page 4 of 6

s2.print();
s1.setter_function();
s1.print();
system("pause");
return 0;
}

Program Output:

Program Code:
// ConsoleApplication8.cpp : Defines the entry point for the console application.
//Copy constructor

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string name;
int age;
int *p;
public:
Page 5 of 6

Student()
{
name = "Umer";
age = 19;
p = new int[3];
int x, y;
for (x = 0; x<3; x++)
{
cout << "Value of quizes = ";
cin >> y;
if (y<0)
{
y = 0;
}
else
{
p[x] = y;
}
}
}

Student(const Student& s){


name = s.name;
age = s.age;
p = s.p;
cout << "Constructor" << endl;
}

void setter_function()
{
string n;
cout << "Name: ";
cin >> n;
name = n;
int x, y;
for (x = 0; x<3; x++)
{

cout << "Marks of quiz= ";


cin >> y;
if (y<0)
{
y = 0;
}
p[x] = y;
}
}
string get_name()
{
return name;

}
int get_age()
{
return age;
}
int* get_pointer()
Page 6 of 6

{
return p;
}
void print()
{
cout << "Name :" << get_name() << endl;
cout << "Age :" << get_age() << endl;
cout << "Pointer address :" << get_pointer() << endl;
}

};

int main()
{
string pn;
Student s1;
Student s2 = s1;
cout << "Object s1: " << endl;
s1.print();
cout << "Object s2" << endl;
s2.print();

s1.setter_function();
s1.print();
system("pause");
return 0;
}
Program Output:

You might also like