|
 |
|
 |
08-11-2008, 01:36 PM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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!
|
|
|
08-11-2008, 07:18 PM
|
#2 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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;
}
|
|
|
08-19-2008, 05:31 AM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
ooops! just noticed
forgot to add code tags!
sorry! Not happen again!
|
|
|
08-30-2008, 12:54 PM
|
#4 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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
* */
|
|
|
09-11-2008, 06:57 AM
|
#5 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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
|
|
|
09-11-2008, 07:33 AM
|
#6 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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();
}
|
|
|
09-11-2008, 07:41 AM
|
#7 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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
|
|
|
09-11-2008, 07:44 AM
|
#8 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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!
|
|
|
09-21-2008, 10:08 AM
|
#9 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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
|
|
|
09-21-2008, 12:56 PM
|
#10 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 735
|
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!
|
|
|
09-21-2008, 01:07 PM
|
#11 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 735
|
As fot Qt... Did you see GTK, FLTK or wxWidgets?
__________________

UT: Ultra-kill... God like!
|
|
|
09-21-2008, 02:17 PM
|
#12 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
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?
|
|
|
09-22-2008, 10:49 AM
|
#13 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 735
|
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!
|
|
|
09-22-2008, 03:38 PM
|
#14 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
thanks! that is interesting!
I just think the C#.NET is interesting...subjective
I'll never buy another MS product!
|
|
|
09-22-2008, 04:16 PM
|
#15 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
Qt4.4 with eclipse is pretty good
Has a built in window designer
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:01 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|