CSI 250L Lab 4 Solution, Spring 2012
American University of Science and Technology · Programming II Lab · 5 pages · Dr. Saeed Raheel · with solution
More CSI 250L files
- Lab 1 Solution · Spring 2012Lab · with solutionView
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
Show the text of this lab
American University of Science & Technology
Department Of Computer Science
CSI 250L – Computer Programming-II Lab
Spring 2011/2012
Lab Work 4
Solution provided by : Dr. Saeed Raheel
Write the class definitions and implementations for the following class specifications.
Use separate files for the definition and implementation.
Publisher
Attributes
ID: a number to identify each publisher.
Name: a string of characters. ( dynamic allocation)
Location: a string of 50 characters.
Type: an integer to specify the type of the publisher, the type can
be 1, 2 or 3.
Methods
Constructor(default values)
Print: a function, to print the information about a
publisher in the following format:
ID = 111, Name = Print_Shop, Location = Lebanon, Type = 1.
Destructor.
Set and get functions.
Write a c++ program to store data about N publishers. The program should do the
following:
Output the information about all publishers of type 1.
Display a sorted list of publisher according to the location.
Change the location “Beirut” to “Saida”.
Display the information about a publisher knowing his ID.
1
//publisher.h
#ifndef PUBLISHER_H
#define PUBLISHER_H
class Publisher
{
private:
int ID;
char *Name;
char Location[50];
int Type;
public:
Publisher(int=0, char* ="", char[]="", int=0);
~Publisher();
void setID(int);
void setName(char *);
void setLocation(char *);
void setType(int);
int getID();
char * getName();
char * getLocation();
int getType();
void print();
};
#endif
//publisher.cpp
#include "Publisher.h"
#include <iostream>
using namespace std;
Publisher::Publisher(int id, char * n, char l[], int t)
{
ID = id;
Name = new char[strlen(n)+1];
strcpy(Name, n);
strcpy(Location, l);
Type = t;
}
Publisher::~Publisher()
{
ID = NULL;
delete[] Name;
delete[] Location;
Type = NULL;
}
void Publisher::setID(int id)
{
ID = id;
}
void Publisher::setName(char*n)
2
{
Name = new char[strlen(n)+1];
strcpy(Name, n);
}
void Publisher::setLocation(char l[])
{
strcpy(Location, l);
}
void Publisher::setType(int t)
{
Type = t;
}
int Publisher::getID()
{
return ID;
}
char *Publisher::getName()
{
return Name;
}
char *Publisher::getLocation()
{
return Location;
}
int Publisher::getType()
{
return Type;
}
void Publisher::print()
{
cout << "ID = " << ID << ", Name = " << Name << ", Location = "
<< Location << ", Type = " << Type << endl;
}
3
//Driver
#include "Publisher.h"
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "How many publisher(s) would you like to create?";
cin >> n;
Publisher *pubs = new Publisher[n];
int id;
char pubname[100];
char publocation[100];
int pubtype;
for (int i=0;i<n;i++)
{
cout << "Enter publisher's ID:";
cin >> id;
cout << "Enter publisher's name:";
cin >> pubname;
cout << "Enter publisher's location:";
cin >> publocation;
cout << "Enter publisher's type:";
cin >> pubtype;
pubs[i].setID(id);
pubs[i].setName(pubname);
pubs[i].setLocation(publocation);
pubs[i].setType(pubtype);
}
cout << "Printing information about publishers whose type = 1" <<
endl;
for (int i=0;i<n;++i)
if (pubs[i].getType() == 1)
pubs[i].print();
cout << endl;
Publisher temppub;
for (int i=0;i<n;++i)
for (int j=0;j<n;++j)
if (strcmp(pubs[j].getLocation(),
pubs[j+1].getLocation())>0)
{
temppub = pubs[j];
pubs[j] = pubs[j+1];
pubs[j+1] = temppub;
}
cout << "After sorting by location : "<< endl;
for (int i=0;i<n;++i)
4
pubs[i].print();
cout << endl;
cout << "Changing location Beirut to Saida : " << endl;
for (int i=0;i<n;++i)
if (strcmp(pubs[i].getLocation(), "Beirut")==0)
pubs[i].setLocation("Saida");
cout << "After changing the location : "<< endl;
for (int i=0;i<n;++i)
pubs[i].print();
cout << endl;
cout << "Please enter the ID of the publisher to prints its
information : ";
cin >> id;
for (int i=0;i<n;++i)
if (pubs[i].getID() == id)
pubs[i].print();
cout << endl;
system("pause");
return 0;
}
5