MedVision ad

C++ (1 Viewer)

Proof

New Member
Joined
Jul 14, 2004
Messages
5
Freedom_Dragon said:
Ive noticed one thing and thats C++.
How come C++ is so popular in uni. So many students uses it[recommended by uni]

Why C++?
C++ has certain characteristics over other programming languages. Most remarkable are:
Object-oriented programming
The possibility to orientate programming to objects allows the programmer to design applications from a point of view more like a communication between objects that on a structured sequence of code. In addition it allows the reusability of code in a more logical and productive way.

Portability
You can practically compile the same C++ code in almost any type of computer and operating system without hardly making changes. C++ is one of the most used and ported to different platforms programming language.

Brevity
Code written in C++ is very short in comparison with other languages, since the use of special characters is preferred before key words, saving effort (and prolonging the life of our keyboards).

Modular programming
An application's body in C++ can be made up of several source code files that are compiled separately and then linked together. Saving time since it is not needed to recompile the complete application when making a single change but only the file that contains it. In addition, this characteristic allows to link C++ code with code produced in other languages like Assembler or C.

C Compatibility
Any code written in C can easily be included in a C++ program without hardly making changes.

Speed
The resulting code from a C++ compilation is very efficient, due indeed to its duality as high-level and low-level language and to the reduced size of the language itself.

http://www.cplusplus.com/info/description.html
 

Thommo

Member
Joined
Sep 18, 2003
Messages
31
Location
Canberra
Could anyone tell me if there is anything that makes C++ unique... i cant find any info on it anywhere. help would be really apreciated.
 

ArrImAPirate

New Member
Joined
Feb 3, 2006
Messages
3
Gender
Male
HSC
2008
I'm a necromancer.



Thommo said:
Could anyone tell me if there is anything that makes C++ unique... i cant find any info on it anywhere. help would be really apreciated.
Partially taken out of "C++: The Complete Reference, fourth edition" by Herbert Schildt

C++ supports OOP (Object Orented Programming).
C++ supports Polymorphism (which is characterised by the phrase "One interface, multiple methods")
C++ supports RTTI (Run Time Type Identification) which is used in OOP classes and structures.
C++ supports Inheritance (Where one class is derived from another (base) class)
C++ allows the declaration of "empty" functions.
Code:
C type declaration: int Foo(void);
C++: int Foo();
C++ (and C99) does NOT default it's data type to an integer if no data type is given.
C++ introduces the bool data type;
C++ introduces namespaces:
Code:
namespace Moo{
function Foo(){ cout << "Moo's Foo!\n";}

};
namespace Meow{
function Foo(){ cout << "Meow's Foo!\n";}

};

using namespace Moo{
Foo();
}

using namespace Meow{
Foo();
cout << "Calling Moo's Foo() from inside Meow.\n"
Moo::Foo();
}
will output
Code:
Moo's Foo!
Meow's Foo!
Calling Moo's Foo() from inside Meow.
Moo's Foo!
C++ introduces classes; this is the most important feature of C++. In order to create an object, you must first define it by using the class keyword. A class is similar syntactically to a structure.
Classes have 3 access levels:
Public - The member may be accessed by anything.
Private - The member may only be accessed by the same object.
Protected - The member may be accessed by any object derived from the class, or may be accessed by its self.

Constructors and Destructors:
Class constructors are called when the class is created, and destructors are called when they are destroyed.

The basic syntax is:
Code:
class _SchoolClass{
public:
int num_of_students;
int num_of_males;
int num_of_females;
char* TeacherName;
char* Subject;

_SchoolClass() {  //Constructer
num_of_students = 30;
num_of_males = 15;
num_of_females = 15;
TeacherName = new char[70];
Subject = new char[30];
sprintf(TeacherName,"Mr. FooBar");
sprintf(Subject,"C++ Programming");
printf("I have created a %s class, being taught by %s; %s's class has %d students: %d males and %d females.", Subject,TeacherName, TeacherName, num_of_students, num_of_males, num_of_females);
}

~_SchoolClass(){  //destructor
printf("%s's %s class has graduated, Huzzah!\n",TeacherName,Subject);
delete TeacherName;
delete Subject;
}
};
this will output
Code:
I have created a C++ Programming class, being taught by Mr. FooBar; Mr. FooBar's class has 30 students: 15 males and 15 females.
Mr. FooBar's C++ Programming class has graduated, Huzzah!
C++ supports overloading.
One way that C++ achieves Polymorphism is through Overloading. Two or more functions can share the same function name sas long as thier parameters are different.
Code:
void Say(){
cout << "I am a mute, because of my lazy programmer.\n"
}

void Say(char* text){
cout << "Say said: \'"<<text<<"\'\n";
}

void Say(int num){
cout << "Your lucky number is "<<num<<".\n";
}

Say();
Say("Mooooo.");
Say(3);
will output:
Code:
I am a mute, because of my lazy programmer.
Say said: 'Moooooo.'
Your lucky number is 3.

I have spent ~30 minutes writing this so I hope it is helpful to others.
There is still much more, but I'm short on time right now.
I may get back and write more later.
 
Last edited:

tama00

New Member
Joined
Mar 26, 2006
Messages
12
Gender
Male
HSC
2006
Some stuff C99 has over C++, dont get me wrong i love c++ aswell but for the sakes of it.

Designated intializers
Compound initializers (kinda same as above)
Variable-length arrays
Flexible array members (this is a great one so heres an example)

Code:
struct foo
{
    int count;
    double averages
    double scores[]; // notice this line.
};
// now create a pointer to it
struct foo *pf;
//and we can set scores so whatever size we want during runtime!

pf = malloc(sizeof(struct foo) + myvarible * sizeof(double));
// now u have an array with an index of whatever you like.
C also have various methods for larger datatypes eg 'unsigned long long' data types, but i think some C++ librarys support that too now..

C was developed in in 1970 something.. i forget, It was developed to expand the UNIX operating system. Its powerful, fast, compact and portable.

Also by C99 i mean C programming 1999 standers if you dont know.
 
Last edited:

ArrImAPirate

New Member
Joined
Feb 3, 2006
Messages
3
Gender
Male
HSC
2008
tama00 said:
Some stuff C99 has over C++, dont get me wrong i love c++ aswell but for the sakes of it.
I'm quite sure (most) C++ compilers include the C99 additions.
That above example could be done via typecasting, too.

Using pointers to variables:

Code:
struct VariableControl {
char* Name;
void* Function;
VariableControl(char* Init_Name) // an overloaded constructor
Name = new char[strlen(Init_Name)+1];
strcpy(Name,Init_Name);
}
VariableControl(){ //default constructor
Name = new char[50];
}
}; // For use in a control panel for changing variables at runtime
   // Note: Changing a variable while it is in use usually makes the value turn to 0; Using a Semiphore or a Mutex(Multithreading Terms) to lock this would be smart to do in an actual Multithreaded app. (I won't be demonstrating this)


VariableControl Vars[5] {
{"FPS_Limit",&FPS_Limit},
{"A_Plain_Variable", 53},
{"A_Read-Only_String","She sells sea shells by the sea shore."}.
{"Another_Global_Var", &GlobalVar},
{"The_Pointer_To_HINSTANCE",&hInst}
};

HINSTANCE hInst;
int Main(HINSTANCE hInstance){
hInst = hInstance;
int FPS_Limit = 45;

//now we access the variables
using namespace std;
cout << Vars[3].Function<<"\n"; // The Read-only string 
cout << "FPS Limit = "<<*Vars[1].Function<<"\n";
if (*Vars[3].Function == FPS_Limit && Vars[3].Function == &FPS_Limit) {
//Proof that Vars[3]'s Function points to FPS_Limit
cout << "*Var[3].Function and FPS_Limit are the same in value and address.\n";
}
return 0;
}
I've used that same concept many times during debugging to help me find the problem with my application, and it works very well.
Although, its not very helpful in a console. I usually put it on another dialog that runs with my application.

The basics of that is:
When passing the VALUE of a variable, value = variable;
When passing the RUN-TIME VARYING VALUE of a variable, Pointer = &Variable; and retrieve the value using Value = *Variable;
 

Slidey

But pieces of what?
Joined
Jun 12, 2004
Messages
6,600
Gender
Male
HSC
2005
Freedom_Dragon said:
Ive noticed one thing and thats C++.
How come C++ is so popular in uni. So many students uses it[recommended by uni]
If you're learning to programme, C and C++ are poor choices to start with. I recommend staying away from them for as long as possible.

I'd reommend starting with something like Haskell or Python.

Haskell is the most unbroken language out there. It's very mathematically sound and quite interesting. It introduces many high-level concepts (recursion, polymorphism, overloading, classes, etc) at the beginner level without making them overly complicated. Also, Haskell is very easy to learn. Two reasons which help explain this are that it is strongly typed, and that it has indentation as punctuation. Strongly typed means that the languages catches you if you try to send an Int to a function that takes Float, it won't compile. This is quite important for error checking or rather, error avoidance. Identation as punctuation means that indentation matters - big time. Semicolons don't exist (but you can include them if you wan't - they're essentially useless), braces don't exist (ever). At first, you might baulk at this. I know I did. But you'll come to realise quite quickly that it is a very cool feature and makes for both neater code and more readable code - which again decreases the chance of errors.

Python is also based on indentation, and very easy to learn and avoid errors. I recommend using it over Perl (and any other scripting language), if you ever have to choose. Also strongly typed.

Java is a good language. It's not bad to start with, but the other two above are much better. Java is also strongly typed - which is important. Java is object oriented, as are Haskell and Python.

Finally, a good reason to start Haskell and Python is that you can type things into the interpreter without having made a programme. Also you can evaluate functions specifically in the interpreter without running the entire programme. This makes learning these languages somewhat easier, as you can see the bones of things more easily.

C and C++ have their place (and you should learn at least one of them eventually). But low-level stuff is a very bad place to start programming. It teaches you bad habbits and isn't typically easy to learn.
 

pungemo

Member
Joined
Mar 19, 2006
Messages
132
Location
Where you least suspect me
Gender
Male
HSC
2006
In regard to what makes C++ uniques:
There is no one function/characteristic that can really make one language unique, only the combination of characteristics and the syntax used. You will find that for anything you may want to do there will be more than one language which can do it. Pirate and Tama00 have already listed most of the characteristics of C++ so just look at them. Slide Rule is right, C++ is a difficult language to learn. I started with Batch, lol, then looked at a high level language called Euphoria. It taught me the basics of high level programming and then C++ was easy.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top