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

please pay attention to the instriction this the homework instruction look at th

ID: 3597848 • Letter: P

Question

please pay attention to the instriction this the homework instruction look at the problem and how to fix it :

Assignment 09

Start Visual C++ Express 2010 and select New Project.

Select Win32 Console Application and name the app Assignment07 and click OK. Use these default screens but make sure you create a project called Assignment09.

Note the path to the directory where it says location. This will be very important in this assignment.

Copy/paste in the following code and compile it to verify that the IDE is working correctly.

// AssignmentA09.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

class Dog {

public:

string name;

public:

// constructor

Dog (string name) {

this->name = name;

cout << "Dog's name is " << name << endl;

}

};

class Cat {

public:

string name;

public:

// constructor

Cat (string name) {

this->name = name;

cout << "Cat's name is " << name << endl;

}

};

int main () {

string name = "Assignment 09";

cout << name << endl;

Dog fido ("Fido");

Cat spot ("Spot");

// These 2 lines of code break the object-oriented paradigm in a major way

cout << "From main, the Dog's name is " << fido.name << endl;

cout << "From main, the Cat's name is " << spot.name << endl;

cout << "Hit any key to continue" << endl;

system ("pause");

return 0;

}

The output will look like this:

Before you do any coding, I want you to study the code and pay attention to the issues relating to scope. There are literally 5 different declarations of the variable:

string name;

How can this be? How can a variable with the same name be used and not conflict with each other? Now is the time to step back and really understand what scope actually means and how it applies to classes and methods.

Also, take a look at the constructors. Here is the Dog constructor.

// constructor

Dog (string name) {

this->name = name;

cout << "Dog's name is " << name << endl;

}

How can a parameter be named the same as a variable in the same class? Note that the parameter is called name as is the variable inside the constructor. And what about this line:

this->name = name;

What is 'this->name'?

Take some time to research all of these questions.

From a design perspective, as the comments in the code indicate, these 2 lines of code break the object-oriented paradigm in a major way.

// These 2 lines of code break the object-oriented paradigm in a major way

cout << "From main, the Dog's name is " << fido.name << endl;

cout << "From main, the Cat's name is " << spot.name << endl;

You should never have direct access to an objects attributes (variables)!!! Take some time to study the meaning of public, private and protected attributes.

The output will look the same. However, the code implementation will change. This is the process by which you make your code more secure.

The Problem

Now, as usual, the problem is simple but the solution may be a bit tough.

First, I want you to fix the access problem listed above. For example, the line should not compile from inside the main method.

fido.name

The first thing that you need to do is change the access designation in the classes to prohibit this from compiling and thus executing. It's like allowing people to have access to passwords within a class. This is actually simple to do since it only takes a change 2 lines of code - but remember that the code won't compile. And this is what you want. You don't want the compiler to let people do things that should not be allowed. That is what good object-oriented design is all about.

Now comes the real problem: add content to the classes (both Dog and Cat) to permit access to the variables so that we can access the name attribute. For now, all I want you to do is provide the access mechanism. In practice, once the access mechanism is in place, you would then add logic to regulate access. For example, let's consider the password scenario.

If you want to change a password, can you do it directly without any security - obviously not. What happens when you attempt to change your password - the system obviously asks for you old password. this is why you can't have direct access to attribute - anyone could change them at will.

So, you need to update the Dog and Cat class so that I can access the name attributes from the main(). But you don't need any logic to regulate them. I will be posting a lot of reading this week so you can explore this problem. You also need to start early and ask me a lot of questions.

Submission

Submit your code in a text file called A09name.txt. For example, mine would be:

there is somthing need to fix inside the main please pay attention to the insruction the homework due is friday

Explanation / Answer

#include <iostream>

#include <string>

using namespace std;

class Dog {

private:

string name; //the variable should be declared private to make it accessible only inside the class

public:

// constructor

Dog (string name) {

this->name = name;

cout << "Dog's name is " << name << endl;

}

string getName() //public function which returns the name of Dog

{

return name;

}

};

class Cat {

private:

string name; //name is private in class, scope = class

public:

// constructor

Cat (string name) { // name as argument , scope = function

this->name = name; // this->name is for the object whose constructor is invoked, this is a pointer and name is local variable

cout << "Cat's name is " << name << endl; //inside constructor , the vaue of name is displayed

}

string getName() //public function which returns the name of Cat

{

return name;

}

};

int main () {

string name = "Assignment 09";

cout << name << endl;

Dog fido ("Fido");

Cat spot ("Spot");

// These 2 lines of code break the object-oriented paradigm in a major way

//cout << "From main, the Dog's name is " << fido.name << endl;

//cout << "From main, the Cat's name is " << spot.name << endl;

//public function getName() should be accessed by main() not the private variables in OOP

cout << "From main, the Dog's name is " << fido.getName() << endl;

cout << "From main, the Cat's name is " << spot.getName() << endl;

cout << "Hit any key to continue" << endl;

system ("pause");

return 0;

}

Output: