Question

2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration....

2 Class Vec Message1

2.1 Introduction

• Write a class Vec Message1 with the following declaration.

class Vec_Message1 {

public:

Vec_Message1();

Vec_Message1(int n);

Vec_Message1(int n, const Message1 &a);

Vec_Message1(const Vec_Message1 &orig);

Vec_Message1& operator= (const Vec_Message1 &rhs);

~Vec_Message1();

int capacity() const;

int size() const;

Message1 front() const;

Message1 back() const;

void clear();

void pop_back();

void push_back(const Message1 &a);

Message1& at(int n);

private:

void allocate();

void release();

int _capacity;

int _size;

Message1 * _vec;

};

2.2 allocate() and release()

• Examine the code of the method allocate() in the class Vec int carefully.

• Edit allocate() to allocate memory of the correct data type.

• Explain why no changes are required in the function body of release().

2.3 Constructors, copy, assign, destroy

• Explain why no changes are required in the function bodies for any of the constructors, copy, assignment and destructor.

• We encapsulated all the code changes in allocate() and release() (actually only allocate()).

• Understand that we are not just learning C++ programming.

• We are also learning software design.

2.4 clear() and pop back()

• Explain why no changes are required in the function bodies for clear() and pop back().

2.5 front() and back()

• The code in the class Vec int for front() and back() returns an array element (if size > 0) and returns 0 otherwise.

int Vec int :: front() const

{ if ( size > 0) . . .

else return 0;

}

int Vec int :: back() const {

if ( size > 0) . . .

else return 0; }

• The items highlighted in red must be changed edited.

• The return type must be changed from int to Message1. This is obvious.

• The statement “return 0” is tricky.

1. We cannot write “return 0” for an object of type Message1.

2. What “zero object” shall we return?

3. Once again, the answer is given by the default constructor.

4. We instantiate a default object and return that.

Message1 Vec_Message1::front() const {

if (_size > 0) ...

else {

Message1 default_obj;

return default_obj;

}

}

Message1 Vec_Message1::back() const {

if (_size > 0) ... else {

Message1 default_obj; return default_obj;

}

}

2.6 Default objects (for your information, not a question)

• A default object of a user-defined class is an object which is created with no user inputs.

• A default object is the analog of an uninitialized variable for a primitive data type such as int or double, etc.

• The default constructor instantiates a default object.

1. By definition, the object has no user inputs.

2. Of course, the default constructor can set values for all the class data members.

3. It can set all the data values to zero or NULL (or blank strings, etc.)

4. But the essential fact is that the calling application does not supply any inputs.

5. A default object is essentially an “empty” object.

2.7 push back(...)

• The code in the class Vec int for push back(int a) contains two lines which must be edited.

void Vec int :: push back(int a) {

. . .

int ?oldvec = vec;

. . .

}

• Edit the code to work correctly for the class Message1. •

Next, when declaring a temporary pointer to save the address of the old array during memory reallocation, the pointer type must be changed from int* to Message1*.

• Both of the above changes should be obvious.

• That’s it.

2.8 at(...)

• The code in the class Vec int for at(int n) returns a reference to an array element if the value of n is valid.

• Else it returns the dereference of a NULL pointer. (Weird, but it works.)

• Edit the code for at(...) to return a pointer of the appropriate type.

2.9 operator []

• Declare two operators as public methods of Vec Message1.

Message1& operator[] (int n); const

Message1& operator[] (int n) const;

1. They are both public.

2. The reason we require two versions of operator[] is so that const objects can invoke the second version.

3. That is why the return type of the second version is a const reference.

• Edit the code to work correctly for the class Message1.?

2.10 Functions reverse(...) and print(...)

• Write the functions reverse(...) and print(...) to operate on an object of type Vec Message1.

void reverse(Vec\_Message1 &v); // input is referemce

void print(ostream &os, const Vec_Message1 &v); // input is const reference

• The function print(...) should run a loop and call operator<< for v[i], for i = 0, . . . , and print the output to os.

// loop i = 0, etc

os << // etc

• Explain why the input to “print(...)” is const but “print(. . . )” itself is not const.?

2.11 Main program

• Write a main program to test your code.

Message1 a, b;

// etc

a = a = b;

a = b = a;

Message1 c(Message1(Message1(a))); // oh yes ?

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

#include<iostream>

using namespace std;

class message1{

int x;

public:

message1(){

x=0;

}

message1(int n){

x=n;

}

int get_x(){

return x;

}

};

class vec_message1{

int _capacity;

int _size;

message1*_vec;

void allocate();

void release();

public:

vec_message1();

vec_message1(int n);

vec_message1(int n,const message1& a);

vec_message1(const vec_message1 &orig);

vec_message1 &operator =(const vec_message1 &rhs);

~vec_message1();

  

const int capacity();

const int size();

message1& front();

message1 back();

  

void clear();

void pop_back();

void push_back( message1& a);

message1&operator[](int n)const;

const message1&operator[](int n);

  

message1& at(int n);

  

  

};

void vec_message1::allocate(){

_vec=new message1[_capacity];

}

void vec_message1::release(){

delete []_vec;

}

vec_message1::vec_message1(){

_size=0;

_capacity=0;

_vec=NULL;

}

vec_message1::vec_message1(int n){

if(n<=0){

_size=0;

_capacity=0;

_vec=NULL;

}

else{

_size=n;

_capacity=n;

allocate();

message1 ob;

for(int i=0;i<_size;i++)_vec[i]=ob;}

}

vec_message1::vec_message1(int n,const message1& a){

if(n<=0){

_size=0;

_capacity=0;

_vec=NULL;

}

else{

_size=n;

_capacity=n;

allocate();

for(int i=0;i<_size;i++)_vec[i]=a;}

}

vec_message1::vec_message1(const vec_message1 &orig){

_capacity=orig._capacity;

_size=orig._size;

allocate();

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

_vec[i]=orig._vec[i];

}

}

vec_message1 & vec_message1::operator=(const vec_message1 &rhs){

_capacity=rhs._capacity;

_size=rhs._size;

release();

allocate();

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

_vec[i]=rhs._vec[i];

}

return *this;

}

void vec_message1 :: clear(){

_size=0;

}

void vec_message1::pop_back(){

if(_size>0){

_size--;

}

}

void vec_message1::push_back( message1& a){

if(_size<_capacity)

_vec[_size++]=a;

else{

_capacity*=2;

message1 *temp=_vec;

allocate();

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

_vec[i]=temp[i];

}

_vec[_size++]=a;

delete[]temp;

}

}

const int vec_message1::capacity(){

return _capacity;

}

const int vec_message1::size(){

return _size;

}

message1& vec_message1::front(){

if(_size<=0){

message1 ob;

return ob;

}

else return _vec[0];

}

message1 vec_message1::back(){

if(_size<=0){

message1 ob;

return ob;}

else return _vec[_size-1];

}

message1 &vec_message1::at(int n){

if(n>=0&&n<_size)return _vec[n];

if(n<0||n>=_size){

message1* ob=NULL;

return *ob;

}

}

void print( vec_message1 &v){

for(int i=0;i<v.size();i++){

cout<<v.at(i).get_x()<<" ";

}

cout<<"\n";

}

void reverse(vec_message1 &v){

if(v.size()==0)return;

message1 temp;

int i=0,j=v.size()-1;

while(i<j){

temp=v.at(i);

v.at(i)=v.at(j);

v.at(j)=temp;

i++;

j--;

}

}

vec_message1 ::~vec_message1(){

_capacity=0;

_size=0;

delete[]_vec;

}

int main(){

vec_message1 v(5,2);

message1 ob1(4),ob2(5);

cout<<"vector : ";

print(v);

cout<<"capacity : "<<v.capacity()<<"\n";

cout<<"size : "<<v.size()<<"\n";

cout<<"front_element : "<<v.front().get_x()<<"\n";

v.push_back(ob1);

cout<<"\nvector : ";

print(v);

cout<<"capacity : "<<v.capacity()<<"\n";

cout<<"size : "<<v.size()<<"\n";

cout<<"front_element : "<<v.front().get_x()<<"\n";

v.push_back(ob2);

cout<<"\nvector : ";

print(v);

cout<<"capacity : "<<v.capacity()<<"\n";

cout<<"size : "<<v.size()<<"\n";

cout<<"front_element : "<<v.front().get_x()<<"\n";

reverse(v);

cout<<"\nReversed vector : ";

print(v);

v.pop_back();

cout<<"\nvector after poping : ";

print(v);

cout<<"capacity : "<<v.capacity()<<"\n";

cout<<"size : "<<v.size()<<"\n";

cout<<"front_element : "<<v.front().get_x()<<"\n";

}

for any query please comment.

please upvote if find it helpful.

Thank you.

Add a comment
Know the answer?
Add Answer to:
2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration....
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
  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Objective: In this assignment you will do the following: (1) manipulate pointers, (2) allocate me...

    Objective: In this assignment you will do the following: (1) manipulate pointers, (2) allocate memory dynamically, (3) implement a default constructor, copy constructor and destructor, (4) use only one pointer to add to the back and to dequeue from the front of the queue. Assignment Description: You will implement a doubly-linked circular queue of integers. Consider the following class declarations when implementing BQUEUE. As always, you must comment your declaration and implementation files, "BQUEUE.h" and "BQUEUE.cpp", respectively. class bqnode {...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • Assuming the following class declaration: class   Course {        private:               string grade_;        &n

    Assuming the following class declaration: class   Course {        private:               string grade_;               int       units_;         public:              Course ( );              Course ( string grade, int units);               ~Course ( );               string get_grade ( ) const ;               int       get_units ( ) const; }; NOTE: Class definitions are not provided but you may assume they're properly defined. Variable names used in your answers must follow our stated naming convention especially for pointer names. Write a fragment of...

  •     class Circle    {    public:        enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN,...

        class Circle    {    public:        enum Color {UNDEFINED, BLACK, BLUE, GREEN, CYAN, RED};        Circle(int = 0, int = 0, double = 0.0, Color = UNDEFINED);        void setX(int);        void setY(int);        void setRadius(double);        void setColor(Color);        int getX() const;        int getY() const;        double getRadius() const;        Color getColor() const;           private:        int x;        int y;   ...

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