Hi everyone,
can you help me please with then following??

a)The C++ Standard Template Library (STL) contains a number of container data types,
explain with suitable examples the advantages these give to the programmer over a
procedural language such as C.

b)Demonstrate using suitable code the usage of the STL std::vector container.

can you help me please with that??
thanks in advance

Recommended Answers

All 15 Replies

Surely you can find (b) on any stl tutorial. I'm sure (a) is there, too. If you provide your draft of an answer, we can verify that it seems reasonable.

This reeks of homework, and nobody will do your homework for you. However, I'll offer some clarification where I feel the questions are incorrect.

explain with suitable examples the advantages these [container classes] give to the programmer over a procedural language such as C.

This question exhibits a common misconception that object-orientated languages are "better", which simply isn't true. "Different", yes. "Better", no. In fact, you'll find that well written procedural code follows most of the guidelines that make up the OO paradigm.

I see only two advantages that the standard C++ container classes have over C. This is certainly not the answer your teacher wants, but I think it's the most correct answer given the perspective of someone proficient with both languages:

  1. They're standardized. C doesn't have any standardized libraries for data structures beyond the native array type.
  2. They're type safe and relatively clean. C offers essentially two ways to create generic containers: pointers to void (which aren't type safe) and the preprocessor (type safe, but a maintenance nightmare). C++'s templates, while still complex, are a huge improvement.

I cant find anything online that explains the advantages with examples between c++ and c, the only I can find is container kinds and data types included which there is no need for them. If you know, please help me.

This reeks of homework, and nobody will do your homework for you. However, I'll offer some clarification where I feel the questions are incorrect.

thanks for your respond, but this is not real my homework, its question from last past exam papers, which probably will not be in this year's paper. But really want to understand some stuff. I am doing an animation course with many directions and mine will not be programming but i have to give this exam either way, my tutors notes are useless.

If still some thinks that is not appropriate to provide any help, its really ok.
Thanks you for the so far responses. :)

If still some thinks that is not appropriate to provide any help, its really ok.

It's not appropriate to provide help unless you prove that you've tried to answer the questions yourself first. Post what you think the answers are and we'll be happy to offer corrections and additions.

Hey Narue, let me ask you something. If you got an offer to write a software,
and your choices were only C and C++. Which would you choose ?

"Different", yes. "Better", no"

Can you show why this is ? Thats a big claim. I understand that OOP programming
can lead to bad software design because of people not knowing the language well
enough. But for the people that are very proficient, in C++. Surely, they can
write very robust and maintainable software using C++, maybe much better than C programming. I see so much advantages that C++ provides over C, yet your claim
is that C++ is not better than C. Is it because, its in a "different" category
perhaps?

I'm just asking out of curiosity, not trying attack you or anything. Thanks

"'Better': No, 'different': Yes" applies to all binary comparisons between programming languages. Only when you specify 'better' about what does it begin to be possible to make a useful answer.

My take: C++ and the STL provide a proven, effective library of tools to get your work done, but there is a larger 'first step' than in C to understand the language and library. The STL, by the way, is not particularly OOP unless you think that specializing templates is OOP (there are arguments each way). Expert programmers in either language will be much more effective and efficient than non-experts in the other language (or the same).

If you got an offer to write a software, and your choices were only C and C++. Which would you choose ?

It depends on the needs of the software. From a very high level view I would most likely choose C++ to leverage the more comprehensive standard library.

I see so much advantages that C++ provides over C

Perhaps you could enumerate those advantages so that I can address them directly rather than dealing with vague terms. I often see people claim advantages in C++ that exist in C as well, or advantages that assume OOP is superior to procedural.

Only when you specify 'better' about what does it begin to be possible to make a useful answer.

Indeed.

The STL, by the way, is not particularly OOP unless you think that specializing templates is OOP (there are arguments each way).

The question was about container classes in particular. Unless you're the kind who thinks it's not OOP until you add inheritance, enough of the bases are covered to refrain from splitting hairs. ;)

>> Perhaps you could enumerate those advantages so that I can address them directly rather than dealing with vague terms

Well, I feel that C++ allows for shorter code written for the programmer by utilizing
the more extensive libraries. C++ code, when written elegantly, is more readable, robust, and is more maintainable. And those few things is what makes
a well written software, a well written software. But I agree that, most C++
programmer who use OOP do not use it properly; I admit, I am one of those right now.

Well, I feel that C++ allows for shorter code written for the programmer by utilizing the more extensive libraries.

C allows for that as well. Or are you suggesting that there aren't extensive libraries available for C? ;) I believe I mentioned the advantage that C++ has a larger standard library, but C probably enjoys more third party libraries.

C++ code, when written elegantly, is more readable, robust, and is more maintainable.

All of those terms are highly subjective.

But I agree that, most C++ programmer who use OOP do not use it properly

I'm not sure how that's relevant to the thread.

>> All of those terms are highly subjective

Point taken. But out of curiosity, from your experience, do you feel as using C++
rather than C, would not only make your life easier, but others maintaining it as well?

>>I'm not sure how that's relevant to the thread.
Its not, but I was throwing that one out there.

from your experience, do you feel as using C++ rather than C, would not only make your life easier, but others maintaining it as well?

Heh, what a loaded question. No, I don't feel that C++ necessarily makes my and maintenance programmer's lives easier. If I did then I probably would be among those claiming that C++ is better than C and C should be retired.

However, I'm also not so stubborn as to use C when C++ (or another language) is better suited to the task.

vijayan121, thanks for the link. Although I see how it could be considered biased (haha), I agree with him completely on all of the points about teaching programming.

Dave

This paper by Stroustrup addresses some of the issues raised in this thread. Well worth a read, IMHO.
http://www.research.att.com/~bs/new_learning.pdf

This paper is good in the context of how to properly teach programming to students, but I don't really see how it is addressing the discussions in this thread. Having learned C++ before C, I can appreciate his point of view, but this in no way nullifies C as the language of choice depending on the situation.

It seems everyone is forgetting about embedded applications. C is used much more frequently than C++ in this area for many reasons. Over 98% of CPUs produced each year are used for embedded applications and the vast majority of those are 8 bit. As a design engineer working mostly with embedded applications, I have rarely chosen C++ as my language of choice. Stroustrup makes the claim that the code has "less" lines, but that is only relevant to the number of lines you see in C vs C++, not assembly or machine code. To demonstrate my point, I wrote the following programs (I used the same string literal on purpose instead of using endl):

test.c

#include <stdio.h>

int main()
{
    printf("hello\n");
    return 0;
}

test.cpp

#include <iostream>

int main()
{
    std::cout << "hello\n";
    return 0;
}

$ gcc test.c -o test1
$ g++ test.cpp -o test2

And just for kicks, I compiled the C program with g++:

$ g++ test.c -o test3

test1 - 11888 bytes
test2 - 14028 bytes
test3 - 12185 bytes

This isn't a perfect example as this version of the compiler is different than the embedded version, but it still demonstrates the point. The size difference is non-trivial in some (most) embedded environments. I designed a high voltage power supply for a company a few years ago. Because of the complexity of the code, I started writing it in C++. After finishing the firmware, I was forced to use a microcontroller that had 16KB of flash instead of 8KB, which added $0.80 of cost to every power supply. It may not seem like much, but they made 50,000 of these power supplies which translates into $40,000 worth of additional expenses. I rewrote the firmware in C and the code size went from 10,014 to 7,994 bytes, thus saving $0.80 per power supply.

There are plenty of other examples like the inflexibility of cout compared to printf, but the point is both languages have advantages and disadvantages. This discussion of one language being superior is a little misguided in my opinion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.