Posted by : Waqas Javed Friday, 27 February 2015

·         Write two classes one named as animal and the other as shape.
·         Think of appropriate data members for both classes.
·         Write setter and getter functions for these classes

·         Check the classes by creating instances of both the classes in the main method and call the print/show method of each instance.

Code

#include<iostream>
#include<string>
#include<string.h>
#include<conio.h>

using namespace std;

class animal{
private:
       string name;
       string color;
string motion;
       bool wings;
       int legs;
       int arms;

public:
       void set_name(string a);
       void set_color(string a);
       void set_motion(string a);
       void set_wings(bool a);
       void set_legs(int a);
       void set_arms(int a);

       string get_name();
       string get_color();
       string get_motion();
       bool get_wings();
       int get_legs();
       int get_arms();

       void display_animal();
};

class shape{
private:
string name;
       string color;
       int no_ofsides;

public:
       void set_name(string a);
       void set_color(string a);
       void set_no_ofsides(int a);

       string get_name();
       string get_color();
       int get_no_ofsides();

       void show_details();
};

void animal::set_name(string a){
       name = a;
}

void animal::set_color(string a){
       color = a;
}

void animal::set_motion(string a){
motion = a;
}

void animal::set_wings(bool a){
       wings = a;
}
void animal::set_legs(int a){
       legs = a;
}
void animal::set_arms(int a){
       arms = a;
}

string animal::get_name(){
       return name;
}
string animal::get_color(){
       return color;
}

string animal::get_motion(){
       return motion;
}
bool animal::get_wings(){
       return wings;

}
int animal::get_legs(){
       return legs;
}
int animal::get_arms(){
       return arms;
}

void animal::display_animal(){
       cout << "The animal name is: " << name << endl
              << "Its color is: " << color << endl
              << "Its movement type is: " << motion << endl;

       if (wings == 0){
              cout << "It does'nt have wings\n";
       }
       else{
              cout << "It have wings\n";
       }

       cout << "It have " << legs << " legs. " << "and" << " " << arms << " arms.\n";
}

void shape::set_name(string a){
       name = a;
}
void shape::set_color(string a){
       color = a;
}
void shape::set_no_ofsides(int a){
       no_ofsides = a;
}
void shape::show_details(){
       cout << "The shape name is: " << name << endl
              << "Its color is: " << color << endl
              << "It has " << no_ofsides << " sides.";
}

string shape::get_name(){
       return name;
}
string shape::get_color(){
       return color;
}
int shape::get_no_ofsides(){
       return no_ofsides;
}

void main(){
       animal a1;
       animal a2;

       shape s1;
       shape s2;

       //setting of a1
       a1.set_name("Cat");
       a1.set_color("Black");
       a1.set_motion("Walk");
       a1.set_wings(0);
       a1.set_legs(4);
       a1.set_arms(0);


       //setting of a2
       a2.set_name("Dog");
       a2.set_color("White");
       a2.set_motion("Walk");
       a2.set_wings(0);
       a2.set_legs(4);
       a2.set_arms(0);

//setting of s1
       s1.set_name("Square");
       s1.set_color("Blue");
       s1.set_no_ofsides(4);

       //setting of s2
       s2.set_name("Pentagon");
       s2.set_color("Red");
       s1.set_no_ofsides(5);

       //display of a1
       cout << "Data of animal 1\n";
       a1.display_animal();

       //display of a2
       cout << "\n\nData of animal 2\n";
       a2.display_animal();

       //display of s1
       cout << "\nData of shape 1\n";
       s1.show_details();

       //display of s2
       cout << "\n\nData of shape 2";
       s1.show_details();
        _getch();
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -