PropellerAds

Archive for March 2015

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
PropellerAds
PropellerAds

Popular Post

Powered by Blogger.

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