Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a makefile that builds executables test1.x and test2.x. Look at the #incl

ID: 3663000 • Letter: C

Question

Create a makefile that builds executables test1.x and test2.x. Look at the #include statements in test1.cpp and test2.cpp to deduce what the intermediate targets and dependencies should be.

Design the class ID, placing the definition in file id.h

Implement the class ID, placing the class implementation in file id.cpp. You can test the code for syntax errors with the command "make id.o" or the command "co3330 id".

Thoroughly test class ID, starting out with the supplied test harnesses in file proj2/test?.cpp using your makefile to build the executables. (Note - you could also use the command line compile scripts "co3330" to create object files and then and "g++ -std=c++11 -otest1.x id.o test1.o" to create executables, as in Project 1.)

Turn in id.h, id.cpp, and makefile using the submit script.

Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu to submit projects. If you do not receive the second confirmation with the contents of your project, there has been a malfunction.

Technical Requirements and Specifications

The class should implement the following diagram:

The class should be a proper type, to include default constructor, 2-argument constructor (that initializes the two data fields), copy constructor, assignment operator, and destructor. Note that the default constructor should set the name to "#" and the age to -1.

The output operator operator<< should be overloaded for the type ID.

Class ID should pass testing with the supplied proj2/test?.cpp with no compile or runtime errors and no compiler warnings when the warning flags -W, -Wall, -Wextra are set.

Building and running the supplied proj2/test?.cpp should result in output identical to the supplied executable area51/IDtest_?.x [? = i or s] .

Hints

The "resource allocation" aspects of this assignment are where most mistakes are made - not just by students doing this assignment, but also throughout the professional world: failure to correctly and safely manage C strings has been at the root of many security leaks and system intrusions over the years.

The distributed "IDTest2ShallowCopy.x" shows one of the things that can go wacky if the programming isn't up to muster. What you see on the screen is:

Note that the internal info in ID objects is changed by an action taken by a client program on its own variables myName and tonyName. These are C strings owned by the test program, which is a client of class ID. The problem is that the ID objects are pointing to the "name" C strings instead of owning a copy of them.

Class Name:
    ID  
Services :
    void        SetName ( const char* ) // sets the name field    void        SetAge  ( int )         // sets the age field    const char* GetName () const        // returns a const pointer to the name field    int         GetAge  () const        // returns the age field by value  
Properties :
    Constructable: objects can be declared as ordinary variables    Assignable:    objects can be assigned one to another    Passable:      objects can be passed by value to and returned as values from functions    
Private variables:
    char * name_  // the name field    int    age_   // the age field  

Explanation / Answer

Here is the code

Header ID.h

#ifndef ID_H
#define ID_H
#include<iostream>


class ID {
public:
ID();
ID(int id, char *name);

void SetName ( char* ); // sets the name field
void SetAge ( int ) ;        // sets the age field
ID( const ID &obj);      // Copy Constructor
~ID(void) ;     // Destructor

const char* GetName () const ;
int GetAge () const ;


void operator= (const ID&);

//std::istream operator>> (std::istream &in, complex& c);
template <typename T>
friend istream& operator>>(istream& os, ID c);
friend ostream& operator<<(ostream& os, const ID &c);
//friend std::ostream &operator<<(std::ostream &out, complex c);

int age;
char *name;
};
#endif // ID_H

ID.cpp

#include "ID.h"
//#include<iostream>
#include <string>
#include <stdio.h>
#include<math.h>

using namespace std;

ID::ID(){
   age=-1; ;
   name="#";
}
ID::ID(int age, char *name){
   this->age=age;
   this->name=name;
}

ID::ID( const ID &obj)
{
   name=obj.name;
   age=obj.age;
}
ID::~ID(void)
{
    cout << "Freeing memory!" << endl;
    delete name;
}

void ID::SetName (char *name)
{
   this->name=name;
}

const char* ID::GetName () const
{
   return this->name;
}

void ID::SetAge ( int a)
{
   this->age=a;
}
int ID::GetAge () const
{
   return this->age;
}


void ID::operator= (const ID& a){
age=a.age;
name=a.name;
}
//bool operator== (complex&);

istream& operator >> (istream &in, ID& a){
in >> a.name >> a.age;
return in;
}


ostream &operator <<(std::ostream& out, const ID& c){
out <<c.name<<' '<< c.age;
return out;
}

int main()
{
  
   ID c1(1,"Asif");
  
   ID c2;
   cout << c1;
   //c1.SetName("Junaid");
  
  
  
   c2.SetAge(12);
   c2.SetName(" Naseer");
  
   ID c3(c2);
  
   c2=c1;
   cout << c2;
  
   cout << c3;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote