answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ddd [48]
2 years ago
6

Write the printitem() method for the base class. sample output for below program: last name: smith first and last name: bill jon

es
Computers and Technology
2 answers:
Kipish [7]2 years ago
8 0

To print the last name as smith,  first and last name as bill jones, use the polymorphism property of OOPs. For this, define a base class and derive the child class from the base class that have the same property as base class have. Set the last name as given name and in the child class assign this value to first name. By using pointer one can call the values. Here I have used stack to store and retrieve the values. The complete code is given below in the further explanation section.

Further explanation:

Code:

The C++ code to print the last name as smith, first and last name as bill jones is as below:

#include <iostream>

#include <vector>

#include <string>

using namespace std;

//Define class  

class BaseClass {

public:

void SetLastName(string givenName) {

lastName =givenName;

}

// Define the print function to print the first name and last name

protected:

string lastName;

};

class DerivedClass : public BaseClass {

public:

void SetFirstName(string givenName) {

firstName = givenName;

}

void PrintItem() {

cout << " first and last name : ";

cout << firstName << " " << lastName << endl;

};

private:

string firstName;

};

int main() {

BaseClass* BaseClassPtr = 0;

DerivedClass* DerivedClassPtr = 0;

vector<BaseClass*> itemList;

int i = 0;

BaseClassPtr = new BaseClass();

BaseClassPtr->SetLastName("Smith");

DerivedClassPtr = new DerivedClass();

DerivedClassPtr->SetLastName("Jones");

DerivedClassPtr->SetFirstName("Bill");

itemList.push_back(BaseClassPtr);

itemList.push_back(DerivedClassPtr);

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

itemList.at(i)->PrintItem();

}

return 0;

}

Output:

last name: smith

first and last name: bill jones

Learn more:

1. A company that allows you to license software monthly to use online is an example of ? brainly.com/question/10410011  

2. How does coding work on computers?  brainly.com/question/2257971 

Answer details:

Grade: College Engineering

Subject: Computer Science

Chapter: C++  Programming

Keyword:

C++, input, output, programming, statements,  loops, if, else, statements, firstname, lastname, base class, derive class, vector, print

ella [17]2 years ago
7 0
The question involves basic polymorphism. The following is the partial flow of the program.

baseItemPtr = new BaseItem();
baseItemPtr.setLastName("Smith");

derivedItemPtr = new DerivedItem();
derivedItemPtr.setLastName("Jones");
derivedItemPtr.setFirstName("Bill");

itemList.add(baseItemPtr);
itemList.add(derivedItemPtr);

for (i = 0; i < itemList.size(); ++i) {
itemList.get(i).printItem();
}

return;
You might be interested in
Darcy is creating an instructional manual for her employees. She entered the text in a word processing program and wants to impo
NeX [460]
When Darcy finish creating the instructional manual for her employees, she can copy paste her content from the word processing program to DTP by :
a) Press Ctrl a to select all the content from the word processing program;
b) Press Ctrl c to copy everything from the word processing program;
c) open DTP
d) Press Ctrl v to paste everything, word by word, page by page from the word processing program to DTP.
5 0
2 years ago
(Displaying a Sentence with Its Words Reversed) Write an application that inputs a line of text, tokenizes the line with String
Serjik [45]

Answer:

I am writing a JAVA and Python program. Let me know if you want the program in some other programming language.

import java.util.Scanner; // class for taking input from user

public class Reverse{ //class to reverse a sentence

public static void main(String[] args) { //start of main() function body

Scanner input = new Scanner(System.in); //create a Scanner class object

   System.out.print("Enter a sentence: "); //prompts user to enter a sentence

   String sentence = input.nextLine(); // scans and reads input sentence

   String[] tokens = sentence.split(" "); // split the sentence into tokens using split() method and a space as delimiter

   for (int i = tokens.length - 1; i >= 0; i--) { //loop that iterates through the tokens of the sentence in reverse order

       System.out.println(tokens[i]); } } } // print the sentence tokens in reverse order

Explanation:

In JAVA program the user is asked to input a sentence. The sentence is then broken into tokens using split() method. split method returns an array of strings after splitting or breaking the sentence based on the delimiter. Here the delimiter used is space characters. So split() method splits the sentence into tokens based on the space as delimiter to separate the words in the sentence. Next the for loop is used to iterate through the tokens as following:

Suppose the input sentence is "How are you"

After split() method it becomes:

How

are

you

Next the loop has a variable i which initialized to the tokens.length-1. This loop will continue to execute till the value of i remains greater than or equals to 0.

for (int i = tokens.length - 1; i >= 0; i--)

The length of first token i.e you is 3 so the loop executes and prints the word you.

Then at next iteration the value of i is decremented by 1 and now it points at the token are and prints the word are

Next iteration the value of i is again decremented by i and it prints the word How.

This can be achieved in Python as:

def reverse(sentence):

   rev_tokens = ' '.join(reversed(sentence.split(' ')))

   return rev_tokens

   

line = input("enter a sentence: ")

print((reverse(line)))

The method reverse takes a sentence as parameter. Then rev_tokens = ' '.join(reversed(sentence.split(' ')))  statement first splits the sentence into array of words/tokens using space as a delimiter. Next reversed() method returns the reversed sentence.  Next the join() method join the reversed words of the sentence using a space separator ' '.join(). If you want to represent the reversed tokens each in a separate line then change the above statement as follows:

rev_tokens = '\n'.join(reversed(sentence.split(' ')))  

Here the join method joins the reversed words separating them with a new line.

Simply input a line of text and call this reverse() method by passing the input line to it.

4 0
2 years ago
Write a code segment that uses an enhanced for loop to print all elements of words that end with "ing". As an example, if words
nadezda [96]

Answer:

 public static void main(String[] args) {

   String ing[] = {"ten","fading","post","card","thunder","hinge","trailing","batting"};

   for (String i: ing){

     if (i.endsWith("ing")){

       System.out.println(i);

    }

   }

 }

Explanation:

The for-loop cycles through the entire list and the if-statement makes it so that the string is only printed if it ends with "ing"

6 0
2 years ago
One factor affecting digital camera quality is the number of pixels, measured in ____, used to store the data for each image.
Vlada [557]
One factor affecting digital camera quality is the number of pixels, measured in megapixels, use to store the data for each image.
5 0
2 years ago
Read 2 more answers
When Russ opened a website on his browser, he saw an error that the site was not compatible with the browser version he was runn
Umnica [9.8K]

Answer:

Patch finders.

Explanation:

Once Russ accessed a webpage on his computer, he had seen an issue that the page was not associated with the browser variant on which he was executing it, although it was important to use patch finders tool to fix the following problems because patch finder is only the software that can resolve the following issues.

3 0
2 years ago
Other questions:
  • how do you make a circuit so 1 switch will turn on/off all the lights(3 lights) and a second switch will change the lights from
    14·2 answers
  • Suppose the algorithms used to implement the operations at layer k is changed. how does this impact services at layers k-1 and k
    10·1 answer
  • What are the two most important network-layer functions in a datagram network? what are the three most important network-layer f
    7·1 answer
  • [20 POINTS] Jaime has to configure the dmz network of his organization to the optimum level. Which is the best practice to do so
    7·2 answers
  • 3.5 Code Practice
    11·2 answers
  • Program documentation _____. Group of answer choices describes the inputs, outputs, and processing logic for all program modules
    6·1 answer
  • Wendy is an attacker who recently gained access to a vulnerable web server running Microsoft Windows. What command can she use t
    9·1 answer
  • Create a program that includes a function called toUpperCamelCase that takes a string (consisting of lowercase words and spaces)
    9·1 answer
  • What is Accenture's role in Multi-party Systems?
    12·1 answer
  • I have a variable and set it equal to 5. 1 then send it as an argument to a function that adds 5 to the variable passed in. Outs
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!