0

Hello,

I have some questions I thought of searching the web and I would be eternally grateful for anyone to answer some of them.

How to properly override the ostream operator?
What exactly is big O notation?
How to count primitive operations?
Is there a way to see how much memory my program uses?
Can I use redirected input in eclipse or Xcode?
What can I truly do with memory management in C++?
Why isn't everything written in c++?
What are the differences between protected/private/public inheritance?
How many ways are there to initialize a primitive data type in C++ and what are they?
Why should you declare a destructor as virtual?
What is an abstract base class?
What is a namespace and how is it used.
What are the differences between a class and a struct in C++, and how does this compare to C?
What is the const operator and how is it used?
What are the differences between passing by reference, passing by value, and passing by pointer in C++?
When is it and when is it not a good idea to return a value by reference in C++?
What is the difference between a variable created on the stack and one created on the heap?
What is a pure virtual function?
What is the STL?
What is the difference between #include <iostream.h> and #include <iostream>?
Why should you never throw an exception in a destructor?
What is the proper way to perform a cast in C++?
2
Contributors
1
Reply
21
Views
2 Years
Discussion Span
Last Post by deceptikon
3

I thought of searching the web

And what did your web search tell you? Some of your questions are easily answered with a web search, so my answers may be somewhat sarcastic. Where the question is trivially found, I won't even bother.

How to properly override the ostream operator?

ostream& operator<<(ostream& out, const MyClass& obj)
{
    // Class-dependent insertion

    return out;
}

Of course, that implies a dependency on std::ostream, which means you can only call this operator<< with narrow streams. A more extensible overload would look like this:

template<typename CharT, typename Traits>
basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& out, const MyClass& obj)
{
    // Class-dependent insertion

    return out;
}

This is the non-member design. If the function requires access to private members, you can make it a friend.

What exactly is big O notation?

A way of calculating the growth rate of algorithms. Click Here.

How to count primitive operations?

Please elaborate.

Is there a way to see how much memory my program uses?

Yes, but it's platform-dependent.

Can I use redirected input in eclipse or Xcode?

Yes, you can redirect from the command line or within code. I'd suggest doing it from the command line as you don't need code changes and in-code redirection can be tricky.

What can I truly do with memory management in C++?

Manage memory...

Why isn't everything written in c++?

Why doesn't everyone use Linux?

What are the differences between protected/private/public inheritance?

The visibility of inherited members is altered:

class A 
{
private:
    int x;
protected:
    int y;
public:
    int z;
};

class B : public A
{
    // x is not visible
    // y is visible only to B and subsequent children
    // z is visible to everyone
};

class C : protected A
{
    // x is not visible
    // y is visible only to C and subsequent children
    // z is visible only to C and subsequent children
};

class D : private A
{
    // x is not visible
    // y is visible only to D
    // z is visible only to D
};

How many ways are there to initialize a primitive data type in C++ and what are they?

In general there are four ways:

int a = 0;
int b(0);
int c{0};
int d = {0};

The third should be preferred in C++11 and later for universal consistency. Prior to C++11, the first is recommended because the second has notable potential for parsing issues where the variable declaration may be interpreted as a function declaration depending on the arguments.

Why should you declare a destructor as virtual?

With the virtual keyword. Any C++ reference will explain the details.

What is an abstract base class?

A class with at least one pure virtual member function. Any C++ reference will explain the details.

What is a namespace and how is it used.

Any C++ reference will tell you this.

What are the differences between a class and a struct in C++, and how does this compare to C?

class and struct are identical except for the default visibility. class is public by default, and struct is private by default. C structures are not classes at all; they correspond to POD types in C++ for compatibility.

What is the const operator and how is it used?

Any C++ reference will explain const.

What are the differences between passing by reference, passing by value, and passing by pointer in C++?

Pass by value copies a value into a temporary or unrelated named variable. Pass by reference creates an alias for the object such that changes to the reference also change the original object. Pass by pointer simulates pass by reference in that it copies the address of the original object into a pointer, where the original object can be changed by dereferencing the pointer.

The biggest difference between pass by reference and pass by pointer is pass by pointer can use a null pointer while pass by reference in general requires a valid object.

Depending on the size of the original object, pass by value may be inefficient due to the cost of making a copy.

When is it and when is it not a good idea to return a value by reference in C++?

The value must continue to exist outside the scope of the called function for returning a reference to work. That's the biggest requirement. Otherwise, general passing recommendations apply.

What is the difference between a variable created on the stack and one created on the heap?

Variables created on the heap are managed by code, and variables created on the stack are managed by the compiler. Further, the stack is limited in size while the heap is theoretically unlimited. However, heap allocation is slower.

What is a pure virtual function?

Something any C++ reference will explain.

What is the STL?

An older term for parts of the standard C++ library that are heavily generalized using templates. Examples include the containers in <vector> and <map>, or algorithms from <algorithm> and <functional>.

What is the difference between #include <iostream.h> and #include <iostream>?

<iostream.h> is the pre-standard version of the <iostream> library. The former is not valid C++, and most modern compilers will reject it. It's also less fully featured, so new code should never use it.

Why should you never throw an exception in a destructor?

Because there's no way to catch it when the destructor is called via unrolling after a prior throw. Click Here.

What is the proper way to perform a cast in C++?

The 'proper' way is to use the casting operators (static_cast, const_cast, reinterpret_cast, dynamic_cast). This way you tailor the cast to your exact needs, and it's easier to search for casts in the code. Opinions vary though, and my belief is that old-style casts are fine.

Votes + Comments
D, you have a lot more patience than I do! :-)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.