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;
}
--------------
"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;
}
Output:
----------------------------------------------------------------------
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;
}
Output:
Q1: Design and write header file and cpp file for the following classes so that provides...
Provide code and full projects neatly and in proper form and in
the correct header and cpp files((we have to make 3 files header
file , one .cpp file for function and one more main cpp file)
Template Classes – Chapter 12
Write an example of a template function that can swap 2 generic
elements.
Create a C++ class vector using the class diagram shown in
Figure 12.2. in Liangs textbook. Do not use the C++
provided header file <vector>...
Using C++ to write .cpp and .h file. Main function and sample
output are given. The task is to write a vector class for dynamic
allocation.declare the the class named vector with the required
attributes. The task are defined in the main function.
#include <iostream> #include "vector.h". #define LOG(x,y) std::cout << x << y « std::endl; #define INFO(x) std::cout << "[INFO]: #define WARNING(x) std::cout <« "[WARNING]: "<< x <« std: :endl; << x << std::endl; " « x < std::endl;...
In header file (.h) and c++ file format (.cpp). A local company has asked you to write a program which creates an Employee class, a vector of Employee class objects, and fills the objects with employee data, creating a "database" of employee information. The program allows the user to repeatedly search for an employee in the vector by entering the employee's ID number and if found, display the employee's data. The Employee_C class should have the following data and in...
.Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...
Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...
C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...
Please write a c++ header file, class implementation file and
main file that does all of the following and meets the requirements
listed below. Also include a Output of your code as to show that
your program works and functions properly.
EXERCISING A DOUBLY-LINKED LIST CLASS
This project consists of two parts, the second of which appears
below. For the first part, write a class that
implements an unordered list abstract data type
using a doubly-linked list with pointers to...
Write a program that will first receive as input the name of an
input file and an output file. It will then read in a list of
names, id #s, and balances from the input file specified (call it
InFile.txt) which you will create from the data provided below. The
program will then prompt the user for a name to search for, when it
finds the name it will output to a file (call it OFile.txt) the
person’s id#, name,...
Write a method that has the following header: public static void printShuffled(String filename) The method reads a text file, filename, sentence by sentence into an array list, shuffles the sentences, and then prints out the shuffled contents. Assume sentences end with one of these characters: ".", ":", "!" and "?". Your method should create an array list for storing the sentences. The array list should be created with approximately the correct number of sentences, instead of being gradually expanded as...
Your assignment is to write a grade book for a teacher. The
teacher has a text file, which includes student's names, and
students test grades. There are four test scores for each student.
Here is an example of such a file:
Count: 5
Sally 78.0 84.0 79.0 86.0
Rachel 68.0 76.0 87.0 76.0
Melba 87.0 78.0 98.0 88.0
Grace 76.0 67.0 89.0 0.0
Lisa 68.0 76.0 65.0 87.0
The first line of the file will indicate the number of students...