CSCI 265: Advanced Programming


An Example Using Make

----------------------shape.h--------------------------------------------------

//shape.h file

#ifndef SHAPE_H
#define SHAPE_H
#include<iostream.h>

//abstract class
class shape{
	public:
		shape(){};
		virtual double area() const = 0;
		virtual double perimeter() const = 0;
		virtual void print_details() const = 0;
		virtual ~shape(){};
};
#endif

---------------------circle.h--------------------------------------------------
//circle.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include<iostream.h>
#include "shape.h"

#define PI 3.14159

//derives from "shape" class

class circle: public shape{
		double radius;	
	public:
		circle(double ip_radius = 0.0);
		virtual double area() const;
		virtual double perimeter() const;
		virtual void print_details() const;
		virtual ~circle();
};
#endif

---------------------circle.cc-------------------------------------------------

//circle.cc

#include<iostream.h>
#include "circle.h"

circle::circle(double ip_radius = 0.0){radius = ip_radius;}

double circle::area() const {return PI * radius;}

double circle::perimeter() const {return 2 * PI * radius;}

void circle::print_details() const{
	cout << "Radius: " << radius << endl;
	cout << "Area: " << area() << endl;
	cout << "Perimeter: " << perimeter() << endl;
}

circle::~circle(){};

---------------------square.h-------------------------------------------------

//square.h

#ifndef SQUARE_H
#define SQUARE_H
#include<iostream.h>
#include "shape.h"

//derives from "shape" class

class square: public shape{
		double side;	
	public:
		square(double ip_side = 0.0);
		virtual double area() const;
		virtual double perimeter() const;
		virtual void print_details() const;
		virtual ~square();
};
#endif

---------------------square.cc-------------------------------------------------

//square.cc

#include<iostream.h>
#include "square.h"

square::square(double ip_side = 0.0){side = ip_side;}

double square::area() const {return side * side;}

double square::perimeter() const {return 4 * side;}

void square::print_details() const{
	cout << "Side: " << side << endl;
	cout << "Area: " << area() << endl;
	cout << "Perimeter: " << perimeter() << endl;
}

square::~square(){};

---------------------shape_main.cc---------------------------------------------

//main program -- driver for the system

#include<iostream.h>
#include "circle.h"
#include "square.h"

main(){
	shape *ptr;
	circle my_circle(4.0);
	square my_square(5.0);

	ptr = &my_circle;
	ptr->print_details();
	
	cout << "-------------------------------------------" << endl;

	ptr = &my_square;
	ptr->print_details();
}

---------------------shape_makefile--------------------------------------------

# Sample makefile for shape program

CC = g++
CFLAGS = -g 
CMD = shape_exec
RM = /bin/rm -f
LP = /usr/ucb/lpr

OBJS = shape_main.o circle.o square.o 
CCFILES = shape_main.cc circle.cc square.cc
HFILES = shape.h circle.h square.h

$(CMD):	$(OBJS)
	$(CC)  -o $(CMD) $(OBJS)

shape_main.o:	shape_main.cc circle.h square.h
	$(CC) $(CFLAGS) -c shape_main.cc 

circle.o:	circle.cc circle.h shape.h
	$(CC) $(CFLAGS) -c circle.cc 

square.o:	square.cc square.h shape.h
	$(CC) $(CFLAGS) -c square.cc 

clean:
	$(RM) $(OBJS) core

print: 
	$(LP)	$(HFILES) $(CCFILES)