0% found this document useful (0 votes)
22 views

Assignment 2 C

This document contains solutions to 4 C++ programming exercises. Exercise 1 involves base and derived classes, accessibility of members, and inheritance. Exercise 2 defines classes related to authors, publishers, and branches with private/public inheritance and member accessibility. Exercise 3 defines a Polygon class with get/set methods and area/parameter calculations, and introduces a Prism class using it. Exercise 4 asks to define a Prism class using composition instead of inheritance from Polygon.

Uploaded by

Dima Azzam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment 2 C

This document contains solutions to 4 C++ programming exercises. Exercise 1 involves base and derived classes, accessibility of members, and inheritance. Exercise 2 defines classes related to authors, publishers, and branches with private/public inheritance and member accessibility. Exercise 3 defines a Polygon class with get/set methods and area/parameter calculations, and introduces a Prism class using it. Exercise 4 asks to define a Prism class using composition instead of inheritance from Polygon.

Uploaded by

Dima Azzam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 2 C++

Dima Fadi Azzam


6302

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”

2nd step: class declaration in a separate file “Polygon.cpp”


#include <cmath>

#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)));
}

3rd step: define a class Prism in a header file “prism.h”

4th step: declaration :


5th step : testing in main():
#include "prism.cpp"

#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

//the definition of polygon is provided in the previous exercise


1st step : class definition in “prism2.h”

2nd step: definition:


3rd step: testing:

You might also like