Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 08-11-2008, 01:36 PM   #1 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
here is an interview question that taught me a lot

Can a virtual function be static?

I added:

(1) yes/no why?
(2) how can one simulate a virtual function being static?

I collect interview question!
landonmkelsey is offline   Reply With Quote
Old 08-11-2008, 07:18 PM   #2 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
another interview question ...true answer discovered years later

code a word histogram in C++

take a file and list words and the number of times they occur in the file

I did the answer in C and wrote a lot of code...this was before I learned STL.

g++ -o histo histo.cpp

answer:

#include <iostream>
#include <iterator>
#include <fstream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

map<string,int> histo;
void record(const string& s)
{
histo[s]++;
}
void print(const pair<const string, int>& r)
{
cout<<r.first<<" , "<<r.second<<endl;
}
int main()
{
ifstream fin("histo.cpp");
istream_iterator<string> is(fin);
istream_iterator<string> eos;
for_each(is,eos,record);
for_each(histo.begin(), histo.end(), print);
return 0;
}
landonmkelsey is offline   Reply With Quote
Old 08-19-2008, 05:31 AM   #3 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
ooops! just noticed

forgot to add code tags!

sorry! Not happen again!
landonmkelsey is offline   Reply With Quote
Old 08-30-2008, 12:54 PM   #4 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
make sure you KNOW the copy constructor

I am not
(1) trying to stump anybody
(2) impress anybody!

This is a learning machine to prepare for a C++ interview or test!

Look through the code and estimate what the value of count will be at the end!

Run the program and see if your estimate matches the value of count!

change fout to cout and see results while the program runs!

Insert cout or fout statements to trace the results!

Code:
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout("result.txt");
struct Foo
{
        static int count;
        Foo()
        {
                fout<<" CTOR"<<endl;
                count++;
        }
        Foo(const Foo& f)                                  //g++ demands the const, VC++ does not
        {
                fout<<"Copy CTOR"<<endl;
                count++;
        }
        Foo operator+(Foo f)
        {
                fout<<"operator + Foo::count = "<<Foo::count<<endl;

                return f;
        }
};
int Foo::count = 0;

Foo add(Foo f1, Foo f2)
{
        return f1 + f2;
}
void f()
{
        fout<<"f 1st Foo::count = "<<Foo::count<<endl;

        Foo f1, f2;

        fout<<"f 2nd Foo::count = "<<Foo::count<<endl;


        add(f1,f2);

        fout<<"f after add() Foo::count = "<<Foo::count<<endl;

}
int main()
{
        f();
        fout<<"Main Foo::count = "<<Foo::count<<endl;
        return 0;
}

/* results in result.txt
 * f 1st Foo::count = 0
 *  CTOR
 *   CTOR
 *   f 2nd Foo::count = 2
 *   Copy CTOR
 *   Copy CTOR
 *   Copy CTOR
 *   operator + Foo::count = 5
 *   Copy CTOR
 *   Copy CTOR
 *   f after add() Foo::count = 6
 *   Main Foo::count = 6    several years ago under microsoft VC++, the answer was 7
 *   */
landonmkelsey is offline   Reply With Quote
Old 09-11-2008, 06:57 AM   #5 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
answer question "can a virtual method be static?"

answer NO!

consider:
#include <iostream>
using namespace std;
class animal {
public:
animal(){cout<<"CTOR animal"<<endl;}
virtual void feed()=0; // no body pure vf
};
class tiger: public animal {
public:
tiger(){cout<<"CTOR tiger"<<endl;}
void feed(){cout<<"feed tiger"<<endl;}
};
class elephant: public animal {
public:
elephant(){cout<<"CTOR elephant"<<endl;}
void feed(){cout<<"feed elephant"<<endl;}
};
class giraffe: public animal {
public:
giraffe(){cout<<"CTOR giraffe"<<endl;}
void feed(){cout<<"feed giraffe"<<endl;}
};
int main() {
cout<<"build zoo"<<endl;
animal* p_zoo[3];
cout<<"assign animals to zoo"<<endl;
p_zoo[0] = new tiger;
p_zoo[1] = new elephant;
p_zoo[2] = new giraffe;
for (int i=0; i<3; i++) p_zoo[i]->feed();
// delete [] p_zoo; // uncomment this and make it work
}

a virtual method requires a pointer for access

a static method requires a class name and double colon (SRO)

a static method or variable can be accessed even if the class has never been instantiated

Simulate static access of virtual method:

hint: write a base class with a virtual method that calls static methods in base and derived classes!

The virtual method should exercise the static method in each base and derived!

try it!

Great question for interview:

Can a derived class access a static method in the base?

Verify with cout!

Consider the issue of hiding!

Last edited by landonmkelsey; 09-11-2008 at 06:58 AM. Reason: remove problem with icon
landonmkelsey is offline   Reply With Quote
Old 09-11-2008, 07:33 AM   #6 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
simulate virtual static code

drat! I forgot code tags on previous post!

g++ -o staticv staticv.cpp (Linux Fedora 9)

Code:
  #include <iostream>
using namespace std;
class Base
{
public:
        virtual int CallStatic()
        {
                BaseStatic();
        }
        static int BaseStatic()
        {
                cout<<"BaseStatic()"<<endl;
        }
};

class Derived:public Base
{

public:
        int CallStatic()
        {
                DerivedStatic();
        }
        static int DerivedStatic()
        {
                cout<<"DerivedStatic()"<<endl;
        }
};
int main()
{
        Base* base = new Base;
        Base* baseDerived = new Derived;
        Derived* derived = new Derived;
        base->CallStatic();
// here the virtual mechanism simulates a virtual static mechanism
        baseDerived->CallStatic();
        derived->CallStatic();
//      Base::BaseStatic();
//      Derived::DerivedStatic();

}
landonmkelsey is offline   Reply With Quote
Old 09-11-2008, 07:41 AM   #7 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
understand thoroughly all the code here to prepare for an interview!

Not all methods exercised!

I threw this together so there could be errors!

compare results with other string classes

Code:
// string1.cpp -- String class methods
#include <iostream>
#include "string1.h"
#include <fstream>
using namespace std;
ofstream fout(".\\results.txt");

// init static class members

int String::num_strings = 0;
const int String::CINLIM = 80;
// static method
int String::HowMany()
{
	return num_strings;
}


String::String(const char * s)     // construct String from C string
{
	fout<<"CTOR = "<<s<<endl;
    len = strlen(s);               // set size
    str = new char[len + 1];       // allot storage
    strcpy(str, s);                // initialize pointer
    num_strings++;                 // set object count
}

String::String()                   // default constructor
{
	fout<<"default CTOR = "<<endl;
    len = 4;
    str = new char[1];
    str[0] = '\0';                 // default string
    num_strings++;
}

String::String(const String & st)
{
	fout<<"copy CTOR = "<<st<<endl;
    num_strings++;             // handle static member update
    len = st.len;              // same length
    str = new char [len + 1];  // allot space
    strcpy(str, st.str);       // copy string to new location
}

String::~String()                     // necessary destructor
{
	fout<<"DTOR = "<<str<<endl;
    --num_strings;                    // required
    delete [] str;                    // required
}

// overloaded operator methods    

// assign a String to a String
String & String::operator=(const String & st)
{
	fout<<"operator = (const String & st)"<<st<<endl;
    if (this == &st)
        return *this;
    delete [] str;
    len = st.len;
    str = new char[len + 1];
    strcpy(str, st.str);
    return *this;
}

// assign a C string to a String
String & String::operator=(const char * s)
{
	fout<<"operator =(const char * s) "<<s<<endl;
    delete [] str;
    len = strlen(s);
    str = new char[len + 1];
    strcpy(str, s);
    return *this;
}


// read-write char access for non-const String
char & String::operator[](int i)
{
    return str[i];
}

// read-only char access for const String
const char & String::operator[](int i) const
{
    return str[i];
}

// overloaded operator friends

bool operator<(const String &st1, const String &st2)
{
    return (strcmp(st1.str, st2.str) < 0);
}

bool operator>(const String &st1, const String &st2)
{
    return st2.str < st1.str;
}

bool operator==(const String &st1, const String &st2)
{
    return (strcmp(st1.str, st2.str) == 0);
}

String operator+(String& strl, const String& strr)
{
	fout<<"operator +(const char * s) "<<strl.str<<" "<<strr.str<<endl;

	String ts;
	delete [] ts.str;
	ts.len = strl.len + 1 + strr.len + 1;
	ts.str = new char[ts.len];
	strcpy(ts.str, strl.str);
	strcat(ts.str, strr.str);
	fout<<"operator+ str = "<<ts.str<<endl;
	fout<<"exit operator +(const char * s) "<<strl.str<<" "<<strr.str<<endl;

	return ts;
}
// simple String output
ostream & operator<<(ostream & os, const String & st)
{
    os << st.str;
    return os; 
}

// quick and dirty String input
istream & operator>>(istream & is, String & st)
{
    char temp[String::CINLIM];
    is.get(temp, String::CINLIM);
    if (is)
        st = temp;
    while (is && is.get() != '\n')
        continue;
    return is; 
}
int main() {
	fout<<"go"<<endl;
	String B = "789";
	fout<<"_________________________"<<endl;
	String A = B + "lmk";
	fout<<"stop"<<endl;
	int i = 9;
	return 0;
}
Code:
// string1.h -- fixed and augmented string class definition
#include <iostream>
using namespace std;
#ifndef STRING1_H_
#define STRING1_H_
class String
{
private:
    char* str;             // pointer to string
	char* temp;
    int len;                // length of string
    static int num_strings; // number of objects
	//    static const int CINLIM = 80;  // cin input limit
    static const int CINLIM;  // cin input limit
	
public:
	// constructors and other methods
    String(const char * s); // constructor
    String();               // default constructor
    String(const String &); // copy constructor
    ~String();              // destructor
    int length () const { return len; }
	// overloaded operator methods    
    String & operator=(const String &);
    String & operator=(const char *);
    char & operator[](int i);
    const char & operator[](int i) const;
	// overloaded operator friends
    friend bool operator<(const String &st, const String &st2);
    friend bool operator>(const String &st1, const String &st2);
    friend bool operator==(const String &st, const String &st2);
    friend ostream & operator<<(ostream & os, const String & st);
    friend istream & operator>>(istream & is, String & st);
	friend String operator+(String &strl, const String& strr);

	// static function
    static int HowMany();
};
#endif
landonmkelsey is offline   Reply With Quote
Old 09-11-2008, 07:44 AM   #8 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
C++ interview questions contain pure "C" questions!

Buy the book "C Puzzlebook" by Feuer and work through it!

I've gotten interview questions out of this little book!

The drill is to be careful and understand why your answer is right or wrong!
landonmkelsey is offline   Reply With Quote
Old 09-21-2008, 10:08 AM   #9 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
I missed C++ too until I started using Qt4.4 under Redhat Linux Fedora 9.

A lot of fun since Qt compiles and runs nearly everywhere!

I am reviewing since C# seems to overrun!

I study C++ interviews and have a 50 page C++ Review

Best to go into a C++ interview with confidence.

I've learned from years of experience/failure!

C++ and now C# are very very big subjects!

Last edited by landonmkelsey; 09-21-2008 at 10:10 AM. Reason: added sentence
landonmkelsey is offline   Reply With Quote
Old 09-21-2008, 12:56 PM   #10 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
C# is not for me. I like C++ and D
D is a new language that overcomes the issues and uses the ideas of Pascal, C++ and Java, and put them in one language... D

Intro - D Programming Language - Digital Mars
__________________

UT: Ultra-kill... God like!
DJMaze is online now   Reply With Quote
Old 09-21-2008, 01:07 PM   #11 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
As fot Qt... Did you see GTK, FLTK or wxWidgets?
__________________

UT: Ultra-kill... God like!
DJMaze is online now   Reply With Quote
Old 09-21-2008, 02:17 PM   #12 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
Qt is far from perfect but it does port to just about any platform.

Comes free with Redhat Fedora 9

There are those who think Smalltalk is #1 but it would be tough finding a job doing Smalltalk... practicalities of life

C# will cause C++ VB and Java to die slowly of attrition. I occasionally see job requiring FORTRAN that one can learn in 5 minutes!

.NET has really gotten complex...up to .NET 3.5

DJMaze: What OS(s) do you use?
landonmkelsey is offline   Reply With Quote
Old 09-22-2008, 10:49 AM   #13 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
Windows:
- 2000
- XP (occasionally, i hate it)
- Vista (at times when someone has issues with his again)

GNU/Linux:
- Fedora 8
- Kubuntu 8.04
- RedHat 9
- CentOS 4

And the odd one: Syllable

I still don't get the .NET hype. Why built an easy 20kb app in .NET that needs:
- 30MB of .NET memory
- massive .NET download/install
- a CPU flops overhead
__________________

UT: Ultra-kill... God like!
DJMaze is online now   Reply With Quote
Old 09-22-2008, 03:38 PM   #14 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
thanks! that is interesting!

I just think the C#.NET is interesting...subjective

I'll never buy another MS product!
landonmkelsey is offline   Reply With Quote
Old 09-22-2008, 04:16 PM   #15 (permalink)
landonmkelsey
Code Monkey
 
Join Date: Aug 2008
Posts: 74
landonmkelsey is on a distinguished road
Qt4.4 with eclipse is pretty good

Has a built in window designer
landonmkelsey is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DB Design Question Part II sde Program Design and Methods 10 09-02-2008 12:37 PM
reference question! quick question, pls help albtross Standard C, C++ 5 08-19-2008 04:58 AM
anyone know a lot? question concerns php also. Rotkiv HTML, XML, Javascript, AJAX 7 01-12-2006 01:09 PM


All times are GMT -8. The time now is 04:01 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting