In: Computer Science
Using ONE of the following themes to create a class and demonstrate the idea. Use this to create a solution (two ADTs and a driver program) to demonstrate the following concepts. Create a one page summary to describe where you are using each of these concepts.
Themes:(PICK ONE)
Concepts to include:
Using the theme: a house has a door (Using C++)
One of the most effective advantage of the object-oriented programming(OOPS) languages is code reuse. And the concepts of inheritance and composition are used to achieve the same.
Here we will look upon the concept of object composition to implement has- a relation in classes.
Composition(HAS-A) is achieved by the use of instance variables that are references to other objects. For example: a house has a door.
Let us define two abstract data types separately for house and door using the classes as ADT in C++.
#include <utility> //Utility class
#include <iostream>
#ifndef HOUSE_H //let there be a header class House.h
#define HOUSE_H //Include guards avoiding double inclusion of the header file
using namespace std;
public class House { //classname
private String location; //data members
private static int doors; //static data member
public House()
{
}
public House(String location,int doors ){ //constructor with default arguments
this.location=location;
this.doors=doors;
}
public String getLocation() { //member functions
return location;
}
public void setLocation(String location) {
this->location = location; //this pointer using arrow operator
}
public static int getNoOfDoors() const { //constant function
return doors; //cannot modify any data member of this object
}
public static void setNoOfDoors(int doors) {
this.doors = abs(doors); //abs is a utility function for absolute value
}
public ~House(){ //Default destructor that does nothing
}
}
public class Door {
//composition has-a relationship
private House house;
public Door(){ //Constructor
this.house=new House();
house.setNoofDoors(10);
}
public int getNoOfDoors() {
return house.getNoOfDoors();
}
public getA()
{
cout <<"\nA"<<endl;
return *this;
}
public getB()
{
cout <<"\nB"<<endl;
return *this;
}
}
//Here is a driver program for composition that uses door object and calculate the total no. of doors in a house.
public class CheckForDoors {
int main() {
Door door();
int noofdoors = door.getNoOfDoors();
cout <<noofdoors<<endl;
door.getA().getB();
}
}
Output:
10
A
B
An ADT comprises of two separate sections:
The classes we have made are separated from its implementation. The classes can be defined as a way of representation for similar kind of objects or instances. Separating the interfaces from its implementation is a good practice for software engineering. We can say a class represents:
toString Method in Java:
The toString() method comes in handy if we have to return the string representation of the object.
toString() method on the object is internally invoked whenever we print any object. We need to override the toString() method according to our use.