0% found this document useful (0 votes)
38 views8 pages

Lab 5

This document provides instructions for a lab assignment on object-oriented programming concepts in C++, including classes, objects, constructors, and arrays. The tasks involve: 1. Creating a membership class with getter/setter methods and constructor overloading to register users as regular or gold members. 2. Correcting errors in an example queue class to demonstrate proper use of getter/setter methods and default constructors. 3. Storing multiple queue objects in an array and accessing values using getter methods.

Uploaded by

umair khalid
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)
38 views8 pages

Lab 5

This document provides instructions for a lab assignment on object-oriented programming concepts in C++, including classes, objects, constructors, and arrays. The tasks involve: 1. Creating a membership class with getter/setter methods and constructor overloading to register users as regular or gold members. 2. Correcting errors in an example queue class to demonstrate proper use of getter/setter methods and default constructors. 3. Storing multiple queue objects in an array and accessing values using getter methods.

Uploaded by

umair khalid
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/ 8

Department of Electrical Engineering

CS212: Object Oriented Programming

Class: BSCS10AB
Lab 05: Classes and Objects and arrays
Instructor: Dr.Shams Qazi
Introduction
This lab is about familiarization with the difference between class and object and constructors.

Objective
To understand the difference between class and object and use of constructor overloading. Students
would be able to use objects in their programming and understand the object oriented paradigm

Tools/Software Requirement

 Microsoft Visual Studio

Description
A class is used to specify the form of an object and it combines data representation and methods for
manipulating that data into one neat package. The data and functions within a class are called members
of the class.

When you define a class, you define a blueprint/conceptual template for a data type. This doesn't
actually define any data, but it does define what the class name means, that is, what an object of the
class will consist of and what operations can be performed on such an object.

Syntax
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main ()
{
Box b1; // Object b1
b1.length = 10;
b1.breadth = 10;
b1.height = 10;
cout << “area=” <<b1.length*b1. breadth *height <<endl;
return 0;
}
Instructions
You are encouraged to use good programming conventions by entering appropriate comments,
using indentations, and using descriptive variable names in your programs. Insert the
solution/answer in this document as directed below. You must also submit this Word document
on the LMS within the time of the lab.

Task 1
Write a program that will Register Two users types in a gym. Regular and membership Gold. The user
will enter Name and age. Then user will choose Membership by pressing 1 or 2 for regular. If user is
regular, don not add its membership. At the end display all details of user according to its data.

Default values for both users will be. (Name: username, age: 00)

Use object, class, getter/setter, constructor and constructor overloading concept.

#include <iostream>
#include <string>
using namespace std;

class membership {
private:
string name = {};
short int age; //short as age is not a large integer
string type = {};
public:
void setname(string str) {
name = str ;
}
void setage(short int x) {
age = x;
}
void settype(char c) {
if (c == '1') type = "Regular";
else if (c == '2') type = "Gold";
}
void getname() {
cout << name;
}
void getage() {
cout << age;
}
void gettype() {
cout << type;
}
void display() {
cout << "name: "<<name<<endl;
cout <<"age: "<<age<< endl;
cout << "membership: "<< type<<endl;
}
void constructor(){ //default values if not registering for membership
name = "username";
age = 0;
type = "none";
}
void constructor(string str, short int x, char c) {
if (c == '1' || c == '2' ) {
name = str;
age = x;
settype(c);
}
else {
membership::constructor();
}
}
};
void main() {
membership user;
char c;
cout << "what type of membership 1 for Regular, 2 for Gold, any number for none: "
<< endl;
cin >> c;
if (c == '1' || c == '2') {
string Uname;
short int Age;
cout << "Enter your name: " << endl;
cin >> Uname;
cout << "Enter your age: " << endl;
cin >> Age;

user.constructor(Uname, Age, c);


user.display();
}
else {
user.constructor();
user.display();
}
}

Task 2

Q2. Identify and eliminate all possible error and bring program in running form. You must do it by
yourself without any help. Add any getter/setter if missing.

#include <iostream>
using namespace std;

class queue
{
int qval;
double pval;

public:

int getval();

queue()
{
qval = 0;
pval = 0.0;
}

void setval(int v)
{
qval = v;
}
void setval(double v)
{
pval = v;
}
};

int queue::getval()
{
return pval;
}

int main()
{
queue obj1(5), obj2(6.0), obj(34322342);
double d;
d = obj3.getval();
obj1.setval(4);
cout << d << obj1.getval() << " " << obj2.getval() << " " << obj1.getval();
}

Corrected code:

#include <iostream>

using namespace std;

class queue
{
int qval;
double pval;
public:

int getqval();
int getpval();

queue()
{
qval = 0;
pval = 0.0;
}

void setval(int v)
{
qval = v;
}
void setval(double v)
{
pval = v;
}
};

int queue::getpval()
{
return pval;
}
int queue::getqval() {
return qval;
}
int main()
{
queue obj1;
obj1.setval(5);
queue obj2;
obj2.setval(6.0);
queue obj3;
obj3.setval(34322342);
int d;
d = obj3.getqval();
obj1.setval(4);
cout << d << obj1.getqval() << " " << obj2.getpval() << " " << obj1.getqval();
}

Task 3

Now from task 2. Use array of 3 objects to store and display values using getters/setters.

#include <iostream>

using namespace std;

class queue
{
int qval;
double pval;

public:

int getqval();
double getpval();

queue()
{
qval = 0;
pval = 0.0;
}

void setval(int v)
{
qval = v;
}
void setval(double v)
{
pval = v;
}
};

double queue::getpval()
{
return pval;
}
int queue::getqval() {
return qval;
}
int main()
{
queue obj[3];
int x, y;
double z;
cin >> x;
cin >> y;
cin >> z;
obj[0].setval(x);
obj[1].setval(y);
obj[2].setval(z);
cout << "list :" << endl;
cout << obj[0].getqval() << endl;
cout << obj[1].getqval() << endl;
cout << obj[2].getpval() << endl;

You might also like