Answer to Question #224832 in C++ for snr

Question #224832

Create a class Vehicle with the following attributes: Registration Number, Model, Manufacturer,

Manufacturing Date, Engine number and Colour. Add a behaviour to change the colour and display

the updated Vehicle details.


1
Expert's answer
2021-08-14T01:40:01-0400
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctime>
using namespace std;
//Implement struct for Date Manifacturing
struct Date
{
	int day;
	int mon;
	int year;
	//Default constructor
	Date()
	{
		this->day = 01;
		this->mon = 01;
		this->year = 1990;
	}
};
//Implement class Vehicle
class Vehicle
{
private:
	int rNum;//Regist Number
	char Model[30];//Model;
	char Manif[50];//Manifacturer
	Date ManDate;//Manifacturing Date
	int EngNum;//Engine Number
	char color[30];//Color
public:
	//Default constructor
	Vehicle()
	{
		this->rNum = 0;
		strcpy(this->color, "Black");
	}
	//Paramitrized constructor
	Vehicle(int _RNum, const char* md, const char* man,int engNum, const char* col)
	{
		this->rNum = _RNum;
		this->EngNum = engNum;
		strcpy(this->Model, md);
		strcpy(this->Manif, man);
		strcpy(this->color, col);
		//Manifacturing Date -set the curent created day
		time_t nw = time(0);
		tm curDay = *localtime(&nw);
		this->ManDate.day = curDay.tm_mday;
		this->ManDate.mon = curDay.tm_mon;
		this->ManDate.year = curDay.tm_year;
	}
	void InputDate()
	{
		cout << "Registration Number: ";
		cin >> this->rNum;
		cout << "Model: ";
		cin >> this->Model;
		cout << "Manifacturer: ";
		scanf("\n");
		fgets(this->Manif, 50, stdin);
		cout << "Engine Number: ";
		cin >> this->EngNum;
		cout << "Manifacturing Day: ";
		cin >> this->ManDate.day;
		cout << "Manifacturing Month: ";
		cin >> this->ManDate.mon;
		cout << "Manifacturing Year: ";
		cin >> this->ManDate.year;
		cout << "Color: ";
		cin >> this->color;
	}
	void OutDate()
	{
		cout << "Reg Num: " << this->rNum << "  |"  << "Model: " << this->Model << "  |";
		cout << "Manifacturer:  " << this->Manif << endl;
		cout << "Manifactiring Date: " << this->ManDate.day << "-" << this->ManDate.mon << "-" << this->ManDate.year << "  |";
		cout << "Engine Num: " << this->EngNum << "  |" << "Color: " << this->color << endl;
	}
	void ChangeColor(const char* newColor)
	{
		strcpy(this->color, newColor);
	}
};
int main()
{
	Vehicle vh;
	vh.InputDate();
	cout << "------------------Data of Vehicle before change color-------------------\n";
	vh.OutDate();
	cout << "\n";
	char buf[30];
	cout << "Please enter new color: ";
	cin >> buf;
	vh.ChangeColor(buf);
	cout << "-----------------After change color----------------------------\n";
	vh.OutDate();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS