New program must use Class and other C++ concepts to represent Structure in the
ID: 3543637 • Letter: N
Question
New program must use Class and other C++ concepts to represent Structure in the sample program.
.A Sample program will be given: problem2_1.cpp
.You should not modify the logic of the program.
-
This means, you should see the same result from both program. Please make sure that your new program works the same as the given program.
problem2_1.cpp code is
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace::std;
// Global named constant
const unsigned int MAX_LENGTH = 255; // maximum length of language name
// Structure
struct P_Language // Contains information of programming language
{
char name[MAX_LENGTH]; // Name of programming language
unsigned type; // Type of programming language
// 1 - Procedural,
// 2 - Object-Oriented,
// 3 - Functional,
// 4 - Logical,
};
// functions
void structInit(P_Language& lang); // Initialize a struct instance
//Precondition: structure lang is defined and
// it and member variables are not initialized.
//Postcondition: lang.name and lang.type are initialized as 'C' and 1 each.
void setName(P_Language& lang); // Set a name
//Postcondition: The value of lang.name is changed as user input.
void setType(P_Language& lang); // Set a type
//Postconditon: The value of lang.type is changed as user input.
void getName(P_Language& lang, char arr[]); // Get a name on arr argument
//Precondition: lang.name is initialized.
//Postconditon: Argument arr will get the value of lang.name.
unsigned getType(P_Language& lang); // Return a type
//Precondition: lang.type is initialized.
//Postconditon: Return the value of lang.type
void show(P_Language& lang); // Display name and type
//Precondition: Member variables of lang are initialized.
//Postconditon: The value of member variables are printed on screen(standard out).
int main(void)
{
// variable declaration
P_Language plang; // structure instance declaration
char temp[MAX_LENGTH]; // buffer for input
structInit(plang);
cout<<"After initialization, the values stored are ---"<<endl;
show(plang);
cout<<"Now we will change the name and type ---"<<endl;
cout<<"(Press enter)"<<endl;
getchar();
setName(plang);
setType(plang);
cout<<endl;
getName(plang, temp);
cout<<"After changing the value,"<<endl;
cout<<"now the name and type of programming language is "<<temp<<" and ";
switch(getType(plang))
{
case 1:
cout<<"Procedural Language";
break;
case 2:
cout<<"Object-Oriented Language";
break;
case 3:
cout<<"Functional Language";
break;
case 4:
cout<<"Logical Language";
break;
default:
cout<<"Unknown Type";
}
cout<<"."<<endl;
return 0;
}
void structInit(P_Language& lang) // Initialize a struct instance
{
strcpy(lang.name, "C");
lang.type=1;
cout<<"Structure instance is declared and initialized!"<<endl;
cout<<"(Press enter)"<<endl;
getchar();
}
void setName(P_Language& lang) // Set a name
{
cout<<"Insert a language name: ";
cin>>lang.name;
}
void setType(P_Language& lang) // Set a type
{
cout<<"Insert a type of language"<<endl;
cout<<"? 1 - Procedural Language"<<endl;
cout<<"? 2 - Object-Oriented Language"<<endl;
cout<<"? 3 - Functional Language"<<endl;
cout<<"? 4 - Logical Language"<<endl;
cout<<":";
cin>>lang.type;
}
void getName(P_Language& lang, char array[]) // Get a name on arr argument
{
strcpy(array, lang.name);
}
unsigned getType(P_Language& lang) // Return a type
{
return lang.type;
}
void show(P_Language& lang) // Display name and type
{
char temp[MAX_LENGTH];
getName(lang, temp);
cout<<"Name of programming language: "<<temp<<endl;
cout<<"Type of programming language: ";
switch(getType(lang))
{
case 1:
cout<<"Procedural Language";
break;
case 2:
cout<<"Object-Oriented Language";
break;
case 3:
cout<<"Functional Language";
break;
case 4:
cout<<"Logical Language";
break;
default:
cout<<"Unknown Type";
}
cout<<endl<<endl;
}
Explanation / Answer
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace::std;
// Global named constant
const unsigned int MAX_LENGTH = 255; // maximum length of language name
// Class
class P_Language // Contains information of programming language
{
private:
char name[MAX_LENGTH]; // Name of programming language
unsigned type; // Type of programming language
// 1 - Procedural,
// 2 - Object-Oriented,
// 3 - Functional,
// 4 - Logical,
public:
// functions
P_Language() // Initialize a class constructor.
//Precondition: class P_Language is defined and
// it and member variables are not initialized.
//Postcondition: P_Language.name and P_Language.type are initialized as 'C' and 1 each.
{
strcpy(name, "C");
type=1;
cout<<"Structure instance is declared and initialized!"<<endl;
cout<<"(Press enter)"<<endl;
getchar();
}
void setName()// Set a name
//Postcondition: The value of P_Language.name is changed as user input.
{
cout<<"Insert a language name: ";
cin>>name;
}
void setType() // Set a type
//Postconditon: The value of lang.type is changed as user input.
{
cout<<"Insert a type of language"<<endl;
cout<<"? 1 - Procedural Language"<<endl;
cout<<"? 2 - Object-Oriented Language"<<endl;
cout<<"? 3 - Functional Language"<<endl;
cout<<"? 4 - Logical Language"<<endl;
cout<<":";
cin>>type;
}
void getName(char array[]) // Get a name on arr argument
//Precondition: lang.name is initialized.
//Postconditon: Argument arr will get the value of lang.name.
{
strcpy(array, name);
}
unsigned getType() // Return a type
//Precondition: lang.type is initialized.
//Postconditon: Return the value of lang.type
{
return type;
}
void show() // Display name and type
//Precondition: Member variables of lang are initialized.
//Postconditon: The value of member variables are printed on screen(standard out).
{
char temp[MAX_LENGTH];
getName(temp);
cout<<"Name of programming language: "<<temp<<endl;
cout<<"Type of programming language: ";
switch(getType())
{
case 1:
cout<<"Procedural Language";
break;
case 2:
cout<<"Object-Oriented Language";
break;
case 3:
cout<<"Functional Language";
break;
case 4:
cout<<"Logical Language";
break;
default:
cout<<"Unknown Type";
}
cout<<endl<<endl;
}
};
int main(void)
{
// variable declaration
P_Language plang; // structure instance declaration
char temp[MAX_LENGTH]; // buffer for input
//structInit(plang);
cout<<"After initialization, the values stored are ---"<<endl;
plang.show();
cout<<"Now we will change the name and type ---"<<endl;
cout<<"(Press enter)"<<endl;
getchar();
plang.setName();
plang.setType();
cout<<endl;
plang.getName(temp);
cout<<"After changing the value,"<<endl;
cout<<"now the name and type of programming language is "<<temp<<" and ";
switch(plang.getType())
{
case 1:
cout<<"Procedural Language";
break;
case 2:
cout<<"Object-Oriented Language";
break;
case 3:
cout<<"Functional Language";
break;
case 4:
cout<<"Logical Language";
break;
default:
cout<<"Unknown Type";
}
cout<<"."<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.