Question

Q1: Design and write header file and cpp file for the following classes so that provides the needs listed for each class. Use

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a Header file with class definiton of Triangle

class Triangle with member variables s1,s2 and s3 for sides of the triangle and count as static to store the count of the nuber of triangle objects created.

Code:

"Triangle.h"

#include<iostream>

using namespace std;

class Triangle

{

    float s1; //side 1

    float s2; //side 2

    float s3; //side s3

    static int count;

    public:

        Triangle()

        {

            count++; //incrementing the object count

        }

        //parameterized constructor

        Triangle(int a,int b,int c)

        {

            s1=a;

            s2=b;

            s3=c;

            count++;

        }

        void readData();

        void print();

};

//initialization of static variable count

int Triangle::count=0;

/*

The method reads the sides of the triangle.

The sides are valid only if the sides are positive and

the side value is less than the sum of other 2 Sides

All the above conditions are checked

*/

void Triangle::readData()

{

    cout<<"\n Enter 3 Sides of the Triangle : ";

    cin>>s1>>s2>>s3;

    while(s1<=0)

    {

        cout<<"\n Enter Valid value for side1 :";

        cin>>s1;

    }

    while(s2<=0)

    {

        cout<<"\n Enter Valid value for side2 :";

        cin>>s2;

        }

    while(s3<=0)

    {

        cout<<"\n Enter Valid value for side3 :";

        cin>>s3;

    }

    while(s1>=(s2+s3))

    {

        cout<<"\n Enter Valid value for side1 :";

      cin>>s1;

    }

    while( s2>=(s1+s3))

    {

        cout<<"\n Enter Valid value for side2 :";

        cin>>s2;

    }

    while(s3>=(s1+s2))

    {

        cout<<"\n Enter Valid value for side3 :";

        cin>>s3;

    }

}

// Print method to print the sides of the Triangle

void Triangle::print()

{

    cout<<" \n Number of triangles in the Program : "<<count;

    cout<<"\n Side 1 : "<<s1;

    cout<<"\n Side 2 : "<<s2;

    cout<<"\n Side 3 : "<<s3;

}

#include<iostream> using namespace std; class Triangle { float s1; //side 1 float s2; //side 2 float s3; //side s3 static intvoid Triangle::readData() { cout<<\n Enter 3 Sides of the Triangle : ; cin>>s1>>52>>53; while(s1<=0) { cout<<\n Enter Vali

--------------

"Triangle.cpp"

#include "Triangle.h"

using namespace std;

int main()
{
Triangle t1;
t1.readData();
t1.print();
Triangle t2,t3(1,2,3);

t2.readData();
  
t2.print();
t3.print();

return 0;
}

#include Triangle.h using namespace std; int main() { Triangle t1; t1.readData(); t1.print(); Triangle t2,t3(1,2,3); t2.rea

Output:

Enter 3 Sides of the Triangle : 5 6 83 Enter Valid value for side3 :8 1 Number of triangles in the Program : Side 1 : 5 Side
----------------------------------------------------------------------

The VectorClass is implemented following member variables and methods.

Variables:

· Character pointer arr

· integer capacity;

· integer current; //size

Functions:

· void push(char data): takes one element and inserts it at the last.

· void push(char data, int index): inserts data at the specified index.

· char get(int index): get the element at the specified index.

· void pop(): deletes the last element.

· int size(): returns the size (i.e,) number of elements in the vector.

· int getcapacity(): returns the capacity of the vector.

· void print(): print vector elements.

Code:

#include<iostream>

using namespace std;

class vectorClass {

    // arr is the integer pointer

    // which stores the address of our vector

    char* arr;

    // capacity is the total storage

    // capacity of the vector

    int capacity;

    // current is the number of elements

    // currently present in the vector (size of the vector)

    int current;

public:

    // Constructor to initialise

    // an initial capacity of 5 element or the size given

    // allocating storage using dynamic allocation

    vectorClass(int size=5)

    {

        arr = new char[size];

        capacity = 5;

        current = 0;

    }

   // Function to add an element at the last

    void push(int data)

    {

        // if the number of elements is equal to the capacity,

        // we double the capacity to hold more elements

        if (current == capacity) {

            char* temp = new char[2 * capacity];

            // copying old array elements to new array

            for (int i = 0; i < capacity; i++) {

                temp[i] = arr[i];

            }

            // deleting previous array

            delete[] arr;

            capacity *= 2;

            arr = temp;

        }

        // Inserting data

        arr[current] = data;

        current++;

    }

    // function to add element at any index

    void push(char data, int index)

    {

        // if index is equal to capacity then this

        // function is same as push defined above

        if (index == capacity)

            push(data);

        else

            arr[index] = data;

    }

    // function to extract element at any index

    char get(int index)

    {

        // if index is within the range

        if (index < current)

            return arr[index];

    }

    // function to delete last element

    void pop()

   {

        current--;

    }

    // function to get size of the vector

    int size()

    {

        return current;

    }

    // function to get capacity of the vector

    int getcapacity()

    {

        return capacity;

    }

    // function to print array elements

    void print()

    {

        for (int i = 0; i < current; i++) {

            cout << arr[i] << " ";

        }

        cout << endl;

    }

};

// Driver code

int main()

{

    vectorClass v;

    v.push('A');

    v.push('B');

    v.push('C');

    v.push('F');

    v.push('G');

    cout << "Vector size : "<< v.size() << endl;

    cout << "Vector capacity : "<< v.getcapacity() << endl;

    cout << "Vector elements : ";

    v.print();

    v.push('E', 1);

    cout << "\nAfter updating 1st index"<< endl;

    cout << "Vector elements : ";

    v.print();

    cout << "Element at 1st index : " << v.get(1) << endl;

    v.pop();

    cout << "\nAfter deleting last element" << endl;

    cout << "Vector size : "<< v.size() << endl;

    cout << "Vector capacity : "<< v.getcapacity() << endl;

    cout << "Vector elements : ";

    v.print();

    return 0;

}

Hinclude<iostream> using namespace std; class vectorclass { // arr is the integer pointer // which stores the address of ourIl function to extract element at any index char get(int index) {| // if index is within the range if (index < current) retur// Function to add an element at the last void push(int data) { // if the number of elements is equal to the capacity, // tha// Driver code int main() { vectorClass v; v.push(A); v.push(B); v.push(C); v.push(F); v.push(G); cout << Vector s

Output:

Add a comment
Know the answer?
Add Answer to:
Q1: Design and write header file and cpp file for the following classes so that provides...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT