Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Topic Video
Question

In C++ code:

Design and write a C++ class that reads text, binary and csv files.  The class functions:

Size:      Returns the file size
Name:      Returns the file name
Raw:    Returns the unparsed raw data
Parse:    A external function to Parse the data.  The function accepts the raw data and returns the data parsed by the function.  This type of function is called a "Call-Back Function". A Call-Back is necessary for each file as each file requires different regular expressions to parse.

 

Call-back.cpp

#include <string>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

string ToLower(string s)
{
    string temp;
    for (char c : s)
        temp.push_back(tolower(c));
    temp.push_back(NULL);
    return temp;
}

string ToUpper(string s)
{
    string temp;
    for (char c : s)
        temp.push_back(toupper(c));
    temp.push_back(NULL);
    return temp;
}

struct Append
{
    string operator()(string a, string b)  // function object
    {
        return a + b;
    }
};

class CallBack
{
    function<string(string)> callback; 
public:
    CallBack(function<string(string)> cb) { callback = cb; }
    string useCallBack(string s) { return callback(s); }
};

void embedd(string s)
{
    CallBack cback(ToUpper);
    cout << cback.useCallBack(s) << endl;
}

void functional()
{
    string cat = "cat";
    string dog = "DOG";
    function<string(string)> fn1 = ToUpper; // ToLower;          // function
    function<string(string)> fn2 = &ToUpper;         // function pointer
    function<string(string, string)> fn3 = Append(); // function object
    function<int(string)> fn4 = [](string s) { return s.length(); };  // lambda expression
    function<int(int, int)> fn5 = plus<int>();      // standard function object

    cout << fn1(dog) << endl;
    cout << fn2(cat) << endl;
    cout << fn3(cat, dog) << endl;
    cout << fn4(dog) << endl;
    cout << fn5(123, 456) << endl;
}

void main()
{
    functional();
    embedd("dog");
}

Expert Solution
Check Mark
Still need help?
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

If we need to put vector in Raw function, how can we do that?

 

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

If we need to put vector in Raw function, how can we do that?

 

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
Computer Engineering
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY