Assignment 2 C
Assignment 2 C
Exercise 1:
1. Base class: class A
Derived class: class C
2. The data members that can be accessed from display() function
are: “m” (both are from the same class C) and int u (C publicly
inherits the class B which contains a protected member “u” that
can be accessed from function within the inherited class (class C)
(then int m and int u are accessed from the display() function)
3. the member functions, which can be accessed from the objects of
class C:
void indata(int, int);
void outdata();
void display();
4. No it cannot, since even though the out() method is public in
class A , due to private inheritance all the members in A (including
protected & public) become private members of B and as a result
the objects can’t directly access the out() function!
Exercise 2:
1. There’s no data members that are directly accessed from the
author object from outside the class since all the attributes in the
“Author class” are private also even though class Author inherits
publicly class Publishers there’s no public members in Publishers
class. on the other hand the class Branch is inherited privately so
even if there’s public data members (attributes) they’ll be private
in Author class which mean they can’t be accessed directly from
the objects of Author class!
2. Branch();
void haveIt();
void giveIt();
3. Functions: start(), display(), author(),branch(), haveIt(), giveIt(), Publishers()
, enter() , display() , registers()
Data members : authorCode , authorName , amount , region , employees ,
p , turnover
Exercise 3:
1st step: define the class PolyGon in the header file “Polygon1.h”
#include "Polygon1.h"
const double PI=3.14159;
int PolyGon::countPol=0;
PolyGon::PolyGon()
{
countPol++;
(this->n)=3;
(this->side)=1;
(this->x)=0;
(this->y)=0;
}
PolyGon::PolyGon(int n,double side){
countPol++;
(this->n)=n;
(this->side)=side;
(this->x)=0;
(this->y)=0;
}
PolyGon::PolyGon(int n,double side,double x,double y){
countPol++;
(this->n)=n;
(this->side)=side;
(this->x)=x;
(this->y)=y;
}
void PolyGon::setSides(int n){
(this->n)=n;
}
void PolyGon::setLength(double length){
(this->side)=length;
}
void PolyGon::setY(double y){
(this->y)=y;
}
void PolyGon::setX(double x){
(this->x)=x;
}
int PolyGon::getSides() const{
return (this->n);
}
double PolyGon::getLength() const{
return (this->side);
}
double PolyGon::getX() const{
return (this->x);
}
double PolyGon::getY() const{
return (this->y);
}
double PolyGon::getParameter() const{
return (this->side)*(this->n);
}
double PolyGon::getArea() const{
return (n*side*side)/(4*tan(PI/(n)));
}
#include <iostream>
int main(){
Prism p1;
Prism *p2=new Prism(2);
Prism p3(4,4,2.5,3,2);
std::cout<<p1.getArea();
std::cout<<p2->getLength();
}
Exersice 4:
a class Prism using composition