CST-157 MST-1 Solution
CST-157 MST-1 Solution
SECTION-A
Answer 1(a)
In C++, We can have more than one constructor in a class with same name, as long as each
has a different list of arguments. This concept is known as Constructor Overloading and is
quite similar to function overloading.
Overloaded constructors essentially have the same name (name of the class) and different
number of arguments.
A constructor is called depending upon the number and type of arguments passed.
While creating the object, arguments must be passed to let compiler know, which constructor
needs to be called.
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
// Two constructors
Point(int x1, int y1) { x = x1; y = y1; }
Point() {x = 0; y = 0; }
int main()
{
Point p1(10, 15); // first constructor is called here
Point p2; // Second constructor is called here
return 0;
}
Output :
p1.x = 10, p1.y = 15
p2.x = 0, p2.y = 0
NEED OF CONSTRUCTOR OVERLOADING WITH REAL TIME EXAMPLE
Real time example scenario of constructor overloading:
Let’s consider a real time example of constructor overloading. We have a music database and
we want to allow users to access song information on below two criteria.
Default user – Get limited information (e.g. title, year and genre etc.) from music database.
Premium user – Get Full information (e.g. title, year, genre, mood, and tempo etc.)
Below is a class “Music” that process song information on the basis of above criteria. We have
2 overloaded constructors in Music class. The Empty constructor Music will process music
information for default (free) users while overloaded constructor with parameter
Music(char* user) will process premium users.
#include <iostream>
using namespace std;
class Music{
public:
/*constructor object to handle default users*/
Music()
{
this->user =getDefaultUser();
//InitDefault(); // do some initialization for default user
}
void querySong()
{
cout<<"processing song information "<<"by "<< this->user <<"\n";
//other codes here
}
private:
char * user;
char *getDefaultUser()
{
return "default user";
}
};
int main()
{
/* Default user object to process songs with limited information*/
Music defaultUser ;
defaultUser.querySong();
return 0;
}
Answer 1(b)
Namespace is a feature added in C++ and not present in C. A namespace is a declarative region
that provides a scope to the identifiers (names of the types, function, variables etc) inside it.
Multiple namespace blocks with the same name are allowed. All declarations within those
blocks are declared in the named scope.
namespace first
{
int val = 500;
}
int main()
{
// Local variable
int val = 200;
return 0;
}
It implies that, in computer programming, namespaces are typically employed for the purpose
of grouping symbols and identifiers around a particular functionality and to avoid name
collisions between multiple identifiers that share the same name.
Answer 1(c)
A friend function violates the concept of data hiding by giving the provision of accessing
private data members to non-member friend function.
Example:
#include <iostream>
class A {
int a;
public:
A() { a = 0; }
void showA(A& x)
{
// Since showA() is a friend, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
int main()
{
A a;
showA(a);
return 0;
}
SECTION-B
Answer 2
https://www.mathopenref.com/coordtrianglearea.html
Given the coordinates of the three vertices of any triangle, the area of the triangle is given
by:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int x[3],y[3],l1,l2,l3;
float area,perimeter,s;
cout<<"Enter x1 and y1-\n";
cin>>x[0]>>y[0];
cout<<"Enter x2 and y2-\n";
cin>>x[1]>>y[1];
cout<<"Enter x3 and y3-\n";
cin>>x[2]>>y[2];
//Using distance formula
l1= sqrt(pow((x[0]-x[1]),2) + pow((y[0]-y[1]),2));
l2= sqrt(pow((x[1]-x[2]),2) + pow((y[1]-y[2]),2));
l3= sqrt(pow((x[2]-x[0]),2) + pow((y[2]-y[1]),2));
perimeter = l1+l2+l3;
s=(float)perimeter/2.0;
area=sqrt(s*(s-l1)*(s-l2)*(s-l3));
cout<<"You entered vertices as:\n";
cout<<"Point 1: "<<x[0]<<","<<y[0]<<endl;
cout<<"Point 2: "<<x[1]<<","<<y[1]<<endl;
cout<<"Point 3: "<<x[2]<<","<<y[2]<<endl;
cout<<"Sides are : "<<l1<<","<<l2<<","<<l3;
cout<<"\nPerimeter : "<<perimeter;
cout<<"\nArea : "<<area<<endl;
return 0;
}
Answer 3
#include <iostream>
using namespace std;
void search(int[],int);
int main()
{
int a[10],num;
cout<<"Enter array elements\n";
for(int i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Enter number to be searched-";
cin>>num;
search(a,num);
return 0;
}
void search(int a[],int n)
{
for(int i=0;i<10;i++)
{
if(n==a[i])
cout<<"Search found at index "<<i;
}
}
Answer 4
#include<iostream>
using namespace std;
class dist2;
class dist1
{
public:
float mtr, cm;
public:
void accept()
{
cout<<"\n Enter Data in Meter & Centimeter : ";
cin>>mtr>>cm;
}
friend void diff(dist1 d1, dist2 d2);
friend void sum(dist1 d1, dist2 d2);
};
class dist2
{
float feet, inch;
public:
void accept()
{
cout<<"\n Enter Data in Feet & Inch : ";
cin>>feet>>inch;
}
friend void difference(dist1 d1, dist2 d2);
friend void sum(dist1 d1, dist2 d2);
};
void difference(dist1 d1,dist2 d2)
{
int n1, n2, n3, ans, m, c, f, in;
n1=d2.inch*2.54;
n2=d2.feet*0.30;
n3=d1.mtr*100;
ans=((d1.cm + n3) - (n1 + n2));
m=ans/100;
c=ans%100;
cout<<"\n --------------------------------------------------------------------";
cout<<"\n Difference in Meters & Centimeters = "<<m<<" mtrs & "<<c<<" cms";
f=m/0.30;
in=c/2.54;
cout<<"\n Difference in Feets & Inches = "<<f<<" feets & "<<in<<" inches";
}
void sum(dist1 d1, dist2 d2)
{
int n1, n2, n3, ans, m, c, f, in;
n1=d2.inch*2.54;
n2=d2.feet*0.30;
n3=d1.mtr*100;
ans=((d1.cm + n3) + (n1+n2));
m=ans/100;
c=ans%100;
cout<<"\n ------------------------------------------------------------------";
cout<<"\n Sum in Meters & Centimeters = "<<m<<" mtrs & "<<c<<" cms";
f=m/0.30;
in=c/2.54;
cout<<"\n Sum in Feets & Inches = "<<f<<" feets & "<<in<<" inches";
}
int main()
{
dist1 d1;
dist2 d2;
d1.accept();
d2.accept();
difference(d1,d2);
sum(d1,d2);
return 0;
}
SECTION-C
Answer 5
#include <iostream>
#include<unistd.h>
#include<iomanip>
using namespace std;
class Traffic
{
bool RED;
bool GREEN;
bool YELLOW;
void display()
{
cout<<"RED :"<<RED<<endl;
cout<<"GREEN :"<<GREEN<<endl;
cout<<"YELLOW:"<<YELLOW<<endl;
}
void change(int flag)
{
if(flag==0)
{
RED = false;
GREEN = true;
YELLOW= false;
}
else if (flag==1)
{
RED = false;
GREEN = false;
YELLOW= true;
}
else
{
RED = true;
GREEN = false;
YELLOW= false;
}
display();
}
public:
Traffic():RED(false), GREEN(false),YELLOW(false)
{}
void timer()
{
int flag=0;
while(1)
{
if(flag<3)
flag++;
else
flag=0;
change(flag);
sleep(10);//lights will change after every 10 seconds
system("cls");
}
}
};
int main()
{
Traffic T1;
T1.timer();
return 0;
}
OR
#include<iostream>
using namespace std;
class trafficLight{
public:
int timer;
trafficLight()
{
timer=0;
}
void RED(int val)
{
cout<<val<<" "<<"RED"<<endl;
}
void GREEN(int val)
{
cout<<val<<" "<<"GREEN"<<endl;
}
void YELLOW(int val)
{
cout<<val<<" "<<"YELLOW"<<endl;
}
};
int main()
{
trafficLight T;
while (T.timer<=100)
{
for(int i=45;i>0;i--)
{
T.RED(i);
T.timer++;
}
cout<<endl;
for(int i=10;i>0;i--)
{
T.GREEN(i);
T.timer++;
}
cout<<endl;
for(int i=5;i>0;i--)
{
T.YELLOW(i);
T.timer++;
}
cout<<endl;
}
}
Actual Program
class OUSB{
private:
unsigned short PORTA;
unsigned short PORTB;
unsigned short PORTC;
public:
OUSB();
char command[256];
unsigned short readPORTA(unsigned short pinNumber);
unsigned short readPORTB();
unsigned short readPORTB(unsigned short newValue);
unsigned short readPORTC();
unsigned short runOUSBcommand(char* command);
};
class TrafficLight{
private:
bool redLamp;
bool yellowLamp;
bool greenLamp;
public:
TrafficLight(){};
TrafficLight(char);
void redOn();
bool isREDon();
void yellowOn();
bool isYELLOWon();
void greenOn();
bool isGREENon();
void changeTrafficLightState();
};
unsigned short OUSB::runOUSBcommand(char *command)
{
FILE *fpipe;
char line[256];
fpipe = (FILE*)_popen(command, "r"); // attempt to open pipe and execute a comma
nd
if (fpipe != NULL) // check that the pipe opened correctly
{
while (fgets(line, sizeof(line), fpipe))
{ // do nothing here, or print out debug data
//cout << line; // print out OUSB data for debug purposes
}
_pclose(fpipe); // close pipe
}
else cout << "Error, problems with pipe!\n";
int ousbOP = (int)atoi(line);
return ousbOP;
}
unsigned short OUSB::readPORTA(unsigned short pinNumber)
{
sprintf_s(command, "ousb −r io porta %d", pinNumber);
PORTA = runOUSBcommand(command);
return PORTA;
}
unsigned short OUSB::readPORTB()
{
PORTB = runOUSBcommand("ousb −r io portb");
return PORTB;
}
unsigned short OUSB::writePORTB(unsigned short newValue)
{
sprintf_s(command, "ousb −r io portb %d", newValue);
PORTB = runOUSBcommand(command);
return PORTB;
}
unsigned short OUSB::readPORTC()
{
PORTC = runOUSBcommand("ousb −r io pinc");
return PORTC;
}
void TrafficLight::redOn()
{
OUSB ousb;
ousb.runOUSBcommand("ousb −r io portb 1");
}
bool TrafficLight::isREDon()
{
OUSB ousb;
if (ousb.readPORTB() == 1)
redLamp = true;
else
redLamp = false;
return redLamp;
}
void TrafficLight::yellowOn()
{
OUSB ousb;
ousb.runOUSBcommand("ousb −r io portb 3");
}
bool TrafficLight::isYELLOWon()
{
OUSB ousb;
if (ousb.readPORTB() == 3)
return true;
else
return false;
}
void TrafficLight::greenOn()
{
OUSB ousb;
ousb.runOUSBcommand("ousb −r io portb 2");
}
bool TrafficLight::isGREENon()
{
OUSB ousb;
if (ousb.readPORTB() == 2)
return true;
else
return false;
}
void TrafficLight::changeTrafficLightState()
{
OUSB ousb;
switch (ousb.readPORTB())
{
case 1:
greenOn();
break;
case 2:
yellowOn();
break;
case 3:
redOn();
break;
default:
cout << "error" << endl;
break;
}
}
int main(int argc, char *argv[])
{
if (argc == 1) // no parameters print this line.
{
cout << "Traffic Light Program\n";
}
else if (argc == 3)
{
OUSB ousb;
TrafficLight light;
int num = atoi(argv[2]);
if (num <= 50 && num >= 0)
{
if (argv[1][0] == 'R')
{
light.redOn();
}
else if (argv[1][0] == 'Y')
{
light.yellowOn();
}
else if (argv[1][0] == 'G')
{
light.greenOn();
}
// User input of the amount of cycles it will go through
for (int i = 0; i < num; i++)
{
Sleep(500);
light.changeTrafficLightState();
}
}
else
cout << "number out of range" << endl;
}
else
cout << "wrong argument inputed" << endl;
return 0;