PropellerAds

Assignment-3 NUST OOP in C++



Write a class “Line” with following specifications
      three data members, endp1, endp2 and char pointer linetype (Give any character value in linetype e.g B implies bold line, N implies normal line and S implies straight line)
      default constructor with default arguments
      copy constructor
      Write getter & setter functions.
      calculateLength method which calculates the length of the line
      compareLine method which compares two line objects and tells which line is longer (use this pointer to use the current objects data members, you can pass 2nd object in the parameter of this function)
      In main program
}  Create line1 object so that default constructor is called
}  Create line2 object using line1 object  (copy constructor must be called)
}  Calculate the length of each line object and compare them by calling functions.



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

using namespace std;

class line{
private:
    int endp1;
    int endp2;
    char *linetype;
public:
    line(int x = 0, int y = 20, char z = 'B'){
        endp1 = x;
        endp2 = y;

        linetype = new char;
        *linetype = z;
    }

    line(const line& l1){
        this->endp1 = l1.endp1;
        this->endp2 = l1.endp2;
        this->linetype = new char;
        *linetype = *l1.linetype;
    }

    void setVal(int x, int y, char z){
        endp1 = x;
        endp2 = y;
        *linetype = z;
    }

    void getVal(){
        cout << "Enp1: " << endp1 << endl<< "Endp2: " << endp2 << endl << "Line Type: " << *linetype << endl;
    }

    void calcLength(){
        cout << "Length of line is: " << endp2 - endp1 << endl;
    }

    void compareL(line &l1){
        if ((this->endp2 - this->endp1) > (l1.endp2 - l1.endp1)){
            cout << "Line is greater then the sended line";
        }
        else if ((this->endp2 - this->endp1) < (l1.endp2 - l1.endp1)){
            cout << "Line is smaller then the sended line";
        }
        else{
            cout << "Lines are equal";
        }
    }
};

void main(){
    line l1;
    line l2(l1);
   
    l1.getVal();
    l1.calcLength();

    cout << endl << endl;

    l2.getVal();
    l2.calcLength();

    cout << endl<< "Setting l2 new values" << endl;

    l2.setVal(10, 15, 'N');
    l2.getVal();
    l2.calcLength();

    cout << endl << "Comparing l1 and l2" << endl;
    l1.compareL(l2);

    _getch();
}
Tuesday 31 March 2015
Posted by Unknown

Assingment 2 Task -2 NUST OOP C++

Create dynamic array you can change the prototype of function where you feel necessary. N is the size of the array

1. void populate_array(int *array, int N);
// this initializes an array of size N with random values.
2. void print_array(int *array, int N);
// this function prints the entire array.
4. void sort (int *array, int N);
//sort the elements of array in ascending order
5. void search (int *array, int N);

//search the element in the array entered by the user.


#include<iostream>
#include<conio.h>
using namespace std;
void populate_array(int *array, int N);
void print_array(int *array, int N);
void search(int *array, int N);
void main(){

                int x;
                cout <<"Enter a number";
                cin >> x;

                int *arr1 = new int[x];

                populate_array(arr1, x);
                print_array(arr1, x);
                search(arr1, x);
                _getch();
}
void populate_array(int *array1, int N){
                for (int i = 0; i < N; i++){
                                *array1 = rand()%100;
                                array1++;
                }
}
void print_array(int *array1, int N){
                for (int i = 0; i < N; i++)
                {
                                cout << *array1 << " ";
                                array1++;
                }
}

void search(int *array1, int N){
                int x;
                bool y = 0;
                cout << "Enter number to search: ";
                cin >> x;

                for (int i = 0; i < N; i++)
                {
                                if (*array1 == x){
                                                cout << "THe elemnt is found at position: " << i+1;
                                                y = 1;
                                               
                                                break;
                                }
                                array1++;
                }

                if (y == 0){
                                cout << "Element is not present in array";
                }
}
Wednesday 4 March 2015
Posted by Unknown

Assingment 2 Task -1 NUST OOP C++

Task-1:

Write a program to implement the Date class which has three data members: day, month and year. Provide two constructors, one which takes arguments and another one which does not (you may set values of data members to 0). Write getter and setter functions (input and show functions) for the class. Write a member function pastDate() which tell where the date is the past date or not.
In main program:
Create two objects using two difference constructors
Call setter and getter functions on those objects.

Call the pastDate() function.


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

using namespace std;

class Date{
private: int day;
                                 int month;
                                 int year;

public:
                //constructors
                Date();
                Date(int, int, int);

                //member funcs
                void setDate(int, int, int);             //set date
                void getDate();                                                                 //print date
                void chkPast(int, int, int);                                                                             //chk date is past or not
};

Date::Date(){
                day = 0;
                month = 0;
                year = 0;
}

Date::Date(int x, int y, int z){
                day = x;
                month = y;
                year = z;
}

void Date::setDate(int x, int y, int z){
                day = x;
                month = y;
                year = z;
}

void Date::getDate(){
                cout << "dd-mm-yy" << endl;
                cout << day << "-" << month << "-" << year << endl;
}

void Date::chkPast(int x, int y, int z){
                if (z < year){
                                cout << x << "-" << y << "-" << z << " is a past date then ";
                }
                else if (z == year && y< month){
                                cout << x << "-" << y << "-" << z << " is a past date then ";
                }
                else if (z == year && month == y && x < day){
                                cout << x << "-" << y << "-" << z << " is a past date then ";
                }
                else if (z == year && month == y && x == day){
                                cout << x << "-" << y << "-" << z << " is a current date" << endl;
                }
                else {
                                cout << x << "-" << y << "-" << z << " is a future date" << endl;
                }
}

void main(){
                Date d1;
                Date d2(10, 12, 1996);

                cout << "Date-1 is:" << endl;
                d1.getDate();

                d1.chkPast(10, 12, 2000);

                cout << endl << endl << "Date-2 is:" << endl;
                d2.getDate();
                d2.chkPast(10, 11, 1994);

                _getch();

}
Posted by Unknown

Assignment 1 NUST OOP C++

·         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();
}
Friday 27 February 2015
Posted by Unknown
PropellerAds
PropellerAds

Popular Post

Powered by Blogger.

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