Question

In: Computer Science

Code a simple Editor class with 2 constructors (one default, one with a string for a...

Code a simple Editor class with 2 constructors (one default, one with a string for a file name)

The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class)

create a short file - at least 5 lines - could be a program

main:

string fileName("input.txt");

Editor miniVi (fileName);

miniVi.displayLines();

C++ explain in detail plz Thank you

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : I have used STL LinkedList to use as linked list, as it is not mentioned to create a linked list class. Please comment below if you have any doubts.

#########################################################################

CODE :

#include<iostream>
#include<fstream>
#include<list>
using namespace std;

//Create a class names "Editor"
class Editor{
   //Create constructors
   public:
       //A constructor is a method with same name as class name
       //Default constructir : doesn't take any arguments
       Editor(){ }
       //Parameterised constructor : takes arguments
       Editor(string filename){
           //Create input file object to read the file
           ifstream inFile;
           inFile.open(filename.c_str());
          
           if(!inFile){
               cout << "File not found..." << endl;              
           }else{
               //Local variable          
               string line;
               //Loops as long as there are lines in the file  
               //Stores each line in 'line' variable
               while(getline(inFile,line)){
                   //Pushes the line to linked list
                   lines.push_back(line);
               }
               //Closes the file
               inFile.close();
           }
       }
       //Method to display all lines of file
       void displayLines(){
           //Iterator to loop trough linked list
           list<string> :: iterator i;
           int k = 1;
           //Loops from beginning to end of the list          
           for(i = lines.begin();i!=lines.end();++i){
               //Displays each value in linked list
               cout << k << " " << *i << endl;
               k++;
           }
       }
       //Method that displays a particular line
       void getLine(){
           //Reads the line number
           cout << "Enter the line number : ";
           cin >> position;
           //eRequired variables
           int k = 1;
           list<string> :: iterator i;
           //Loops from beginning to the end of the linked list
           for(i = lines.begin();i!=lines.end();++i){          
               //If k value matches position displays the line  
               if(k == position)                  
                   cout << *i << endl;
               //Increments k by 1
               k++;
           }
       }
   //Create private instance variables
   private:
       //Linked list of strings (I have used STL List)
       list<string> lines;
       int position;
};
int main(){
   //Testing the code against the below lines given in question
   string fileName("input.txt");
   Editor miniVi (fileName);
   miniVi.displayLines();
   miniVi.getLine();
   return 0;
}

############################################################

input.txt

#include<iostream>
using namespace std;
int main(){
   return 0;
}

#####################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

##########################################

input.txt

##########################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

Create a class called College that has: At least 2 constructors (one should be the default...
Create a class called College that has: At least 2 constructors (one should be the default constructor) All members private The relevant setters and getters A print_me() function A college_ID member (unique) Write two unsorted lists that manage the data for colleges and their ranking: An unsorted list using an array An unsorted list using a linked list Each college will have a unique ID number that will be used for search. For the lab you don't need to read...
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
1. define a class that has three different constructors. 2. write code that overrides the following...
1. define a class that has three different constructors. 2. write code that overrides the following method public double sqr(double num1) { return num1*num1; }
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors...
summer/** * This Object-Oriented version of the "Summer" class * is a simple introduction to constructors / * private data members / static vs. not static / and the * "toString" method. * * SKELETON FOR LAB TEST. * * @author Raymond Lister * @version April 2015; */ public class SummerOO { public static int numSummers = 0; // The above variable is used to count the number of // instances of the class SummerOO that have been created. //...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.
Inheritance Constructors Accessibility Accessibility Rules private         accessible within the same class. unspecified (friendly) (default)         additionally...
Inheritance Constructors Accessibility Accessibility Rules private         accessible within the same class. unspecified (friendly) (default)         additionally accessible to classes within the same package (folder). protected         additionally accessible to child classes. public         accessible to all classes. Description This assignment has two parts. Do the part I first and test it using the data in test run 1. Then do the second part and test it using data in test run 2 and test run 3. Submit the output from test run...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output: 6.0...
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor)...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor) Getter/setters Math Type cast Friend << and >> Part-2 for the explanation for the behavior observed by running the provided test pattern: six (6) operations in one statement vs. six (6) operations in six (6) statements. PROJECT 1 Frac Class The following information, located on Github m03, are provided as a starter. Frac.h, a complete Frac Class Declaration ( the definition is to be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT