answersLogoWhite

0

📱

C++ Programming

Questions related to the C++ Computer Programming Language. This ranges all the way from K&R C to the most recent ANSI incarnations of C++, including advanced topics such as Object Oriented Design and Programming, Standard Template Library, and Exceptions. C++ has become one of the most popular languages today, and has been used to write all sort of things for nearly all of the modern operating systems and applications." It it a good compromise between speed, advanced power, and complexity.

2,546 Questions

What are the private data members of kilometer class in c plus plus?

C++ does not have a built-in kilometre class. If you have such a class, it was provided for you by a third party. However, your IDE should be able to show you the members, including private members. I'd imagine such a class would have a private float or double member to store the actual number of kilometres it represents, and perhaps some public methods to convert the number to other formats, such as millimetres, centimetres, miles, yards, inches, etc.

Are accessor member functions a sign of poor class design?

No, accessor member functions are a sign of good class design, particularly in terms of data encapsulation.

What do you mean by forward declarations of a class in c plus plus?

Which came first, the chicken or the egg? The answer is, of course, the egg. After all, birds evolved long after egg-laying reptiles, so eggs had to have come first. But what exactly does that have to do with forward declarations? Well, everything, as it turns out. Forward declarations are essential whenever two classes depend on each other.


As you know, in C++ you must define a type before you can use it. So let's define a simple chicken and egg:

#include


class Chicken

{

public:

Chicken(Chicken* parent=0):m_pParent(parent){}

Egg* lay_egg();

private:

Chicken* m_pParent;

};


class Egg

{

public:

Egg(Chicken* parent=0): m_pParent(parent){}

Chicken* hatch();

private:

Chicken* m_pParent;

};


Egg* Chicken::lay_egg()

{

return(new Egg(this));

}


Chicken* Egg::hatch()

{

return(new Chicken(m_pParent));

}


int main()

{

Chicken chicken;

Egg* egg = chicken.lay_egg();

Chicken* chick = egg->hatch();

Egg* egg2 = chick->lay_egg();

delete(egg2);

egg2=0;

delete(chick);

chick=0;

delete(egg);

egg=0;

return(0);

}

Straight away there's a problem. The compiler won't allow this because our chicken lays eggs but the definition of an egg appears after the definition of a chicken. Ah, but of course -- eggs came first! So let's swap the definitions around:

#include


class Egg

{

public:

Egg(Chicken* parent=0): m_pParent(parent){}

Chicken* hatch();

private:

Chicken* m_pParent;

};


class Chicken

{

public:

Chicken(Chicken* parent=0):m_pParent(parent){}

Egg* lay_egg();

private:

Chicken* m_pParent;

};


Egg* Chicken::lay_egg()

{

return(new Egg(this));

}


Chicken* Egg::hatch()

{

return(new Chicken(m_pParent));

}


int main()

{

Chicken chicken;

Egg* egg = chicken.lay_egg();

Chicken* chick = egg->hatch();

Egg* egg2 = chick->lay_egg();

delete(egg2);

egg2=0;

delete(chick);

chick=0;

delete(egg);

egg=0;

return(0);

}

Hmm. The compiler's still not happy. Our eggs need to hatch chickens but, again, the definition of a chicken now appears after the definition of an egg. We seem to have a catch-22 situation. No matter which order we define them, we simply cannot emulate a simple chicken and an egg.


The answer is, you guessed it, to use a forward declaration:

#include


class Chicken; // forward declaration!


class Egg

{

public:

Egg(Chicken* parent=0): m_pParent(parent){}

Chicken* hatch();

private:

Chicken* m_pParent;

};


class Chicken

{

public:

Chicken(Chicken* parent=0):m_pParent(parent){}

Egg* lay_egg();

private:

Chicken* m_pParent;

};


Egg* Chicken::lay_egg()

{

return(new Egg(this));

}


Chicken* Egg::hatch()

{

return(new Chicken(m_pParent));

}


int main()

{

Chicken chicken;

Egg* egg = chicken.lay_egg();

Chicken* chick = egg->hatch();

Egg* egg2 = chick->lay_egg();

delete(egg2);

egg2=0;

delete(chick);

chick=0;

delete(egg);

egg=0;

return(0);

}

Now the code compiles!


The forward declaration simply acts as a sort of place-holder. We're just telling the compiler that although we aren't quite ready to define a chicken, one will be defined at some point -- it may even be in a completely different file. But that is enough to appease the compiler, it can simply fill in the blanks when our chicken is fully defined.


This type of scenario crops up quite a lot, especially when working with parent and child classes that must depend on each other, just like our chicken and egg. However, we normally design our classes using separate source files each with their own header file, and that would then make it impossible for our chicken and egg header's to include each other's header. Instead, we must use forward declarations in the headers, and place the corresponding #include directives in the source files.

What are default arguments in c plus plus?

Default arguments are function parameters for which a default value is implied when not explicitly stated.

int foo(int x, int base=10 ) {

return( x%base); }

The above function assumes 'base' is 10 unless you specify otherwise when making the call. Thus calling foo(15) will return 5, as will foo(5,10), but foo(15,16) will return 15.

Note that default parameters must appear after all non-default parameters in a function declaration. Once you specify a default parameter, all other parameters that follow must also have default values.

Note also that when the definition of a function is split from its declaration, only the declaration should declare the default parameters:

// Declaration:

int foo(int x, int base=10 );

// Definition:

int foo(int x, int base ) {

return( x%base); }

How do you write a Turbo C plus plus program that reads sides of triangle?

You write it exactly the same as you would write it in any other verions of C++, by taking user input to determine the three sides of your triangle. In other words, input three real numbers. What you do with those three numbers is entirely up to you, but presumably you'd want to calculate the angles of a triangle given the length of its three sides. For that you would need to use the cosine rule which states that for any triangle with angles A, B and C whose opposing sides are a, b and c respectively, cos A = (b2 + c2 - a2)/2bc and cos B = (c2 + a2 - b2)/2ca. Knowing two angles, A and B, you can easily work out that angle C must be 180 - (A + B).

How do you sort a list of strings alphabetically using a two-dimensional character array?

Firstly you must determine the longest string in the list and allocate an array to accommodate it. This may incur a lot of wasted memory, especially if you have a particularly long string amongst hundreds or thousands of very short strings. A more efficient method would be to create a one-dimensional array of pointers to null-terminated strings. This has the advantage that you do not need to copy or move the strings, only the pointers need be sorted, and the strings can reside anywhere in memory -- they needn't be in contiguous memory as they would in a two-dimensional character array.

Regardless, once you've got your array, there are many different algorithms you can employ to sort it. Some are better than others. For short lists up to perhaps 20 items, a Bubble Sort is ideal and is by far the simplest to implement. For larger lists, you should consider more efficient sorting algorithms, such as Quick Sort and Merge Sort. See the related link below for a comprehensive list and table of comparisons. You will also find example code and full explanations on each algorithm, including pros and cons of each.

What is rand function in c plus plus?

The rand() function returns an integer (int) value that represents a pseudo-random number between 0 and RAND_MAX, RAND_MAX being a constant declared in the run-time library. Each time rand() is invoked, a different value is returned.

How can we determine number of objects in a file by Using a c plus plus program to support your approach?

If all objects are the same length, divide the size of the file by the size of an object. If the objects are variable length or different types of object, you must serialise the objects one at a time, counting them as you go. A better approach to counting is to maintain a count of stored objects in an user-defined header segment within the file, usually at the start of the file, or if dealing with mixed data types, store the length of each object before the data for each object, thus allowing you to quickly traverse the file without serialising.

What are main uses of c and c plus plus?

The primary use of C is to write efficient, cross-platform, procedural code. It is much easier to write cross-platform machine code using C than it is to use assembly, since C also permits procedure calls and structured programming. C is regarded as a mid-level language, giving the benefits of low-level coding combined with high-level abstraction.

The primary use of C++ is to write efficient, cross-platform, object-oriented code. Object-oriented programming extends structured programming allowing highly-complex data to be modelled far more easily and more robustly than with structured languages. Since C++ evolved from C, you also gain the benefit of using C-style coding when required.

C is an excellent language for writing low-level code that would otherwise be written in assembly. This includes device drivers and operating system components. Major applications are typically written in C++.

What is the maximum value you can store in an int data type in c sharp?

Unlike C and C++, all built-in data types in C# are not implementation-defined, they are consistent across all implementations. An int always occupies 32-bits, thus the range of valid values is -2,147,483,648 to 2,147,483,647 inclusive.

Definition of coments and its types in c plus plus?

Comments in C++ always begin with // and extend to the end of the line. Multi-line comments must have // on each line. Any text that follows this symbol up to the newline character is completely ignored by the compiler.

You can also use C-style comments, where multi-line comments can be enclosed within opening /* and closing */ markers. This type of comment can also be used to insert a short comment between C++ expressions upon the same line. Again, the compiler ignores everything, from the opening /* marker up to the closing */ marker.

All comments are stripped from your source during preprocessing, at the point where macros are processed (also known as precompilation). The resulting intermediate file is the file that is actually compiled.

How do you make a stickman move in C plus plus programming?

C++ has no built-in graphics support whatsoever. C++ is a generic language designed to operate on a wide variety of hardware, including devices without graphics capability. Graphics are platform-specific, thus the code you use will be dependant upon the graphics API specific to your operating system and its hardware. Although you could use the text-based output of your console to draw and move a stick figure, the console was never intended to be used in this way and you'd actually find it a lot easier to use a proper graphics library, where line-drawing is greatly simplified by a huge range of abstract methods that exploit the low-level capabilities of your hardware.

What enables an object to initialize itself when it is created?

Object initialisation is taken care of by the object's constructor(s). Initialisation is generally done during the initialisation stage of the constructor (immediately before the body of the constructor). Complex members such as pointers can be initialised to NULL in the initialisation stage. If new memory must be allocated to a member pointer, do it in the body of the constructor. Remember to clean up all allocated memory in the class destructor.

class cMyClass

{

public: // Construction.

cMyClass(); // Default constructor.

cMyClass(const cMyClass & MyClass); // Copy constructor.

cMyClass(const int i); // Overloaded constructor.

~cMyClass(); // Destructor.

private: // Member variables.

int m_int; // An integer.

int * m_pInt; // A pointer to an integer.

};

// Default constructor.

cMyClass::cMyClass(): // Colon indicates initialisation stage.

m_int( 0 ), // Comma-separated list of members to be initialised.

m_pInt( NULL ) // Optional; m_pInt will be initialised in the body.

{

// Body of the constructor:

// m_int is currently initialised to the value zero.

// m_pInt currently points to NULL.

m_pInt = new int;

*m_pInt = 0;

// m_pInt now points to new memory containing the value zero.

// Initialisation is complete!

}

// Copy constructor

cMyClass::cMyClass(const cMyClass & MyClass): // Initialisation

m_int( MyClass.m_int ), // Member list

m_pInt( NULL )

{

// m_int is initialised to the value of MyClass.m_int

// m_pInt currently points to NULL.

m_pInt = new int; // Allocate new memory.

*m_pInt = *MyClass.m_pInt; // Deep-copy the value.

// m_pInt now points to a copy of the value pointed to by MyClass.m_pInt.

// Initialisation is complete!

}

// Overloaded constructor

cMyClass::cMyClass(const int i): // Initialisation.

m_int( i ), // Member list.

m_pInt( NULL )

{

// m_int is initialised to the value of i.

// m_pInt currently points to NULL.

m_pInt = new int;

*m_pInt = 0;

// m_pInt now points to new memory containing the value zero.

// Initialisation is complete!

}

// Destructor. cMyClass::~cMyClass()

{

// Clean up allocated memory.

if( m_pInt ) delete m_pInt;

}

Write a program to return a function using class in C Plus Plus?

Classes cannot return values, only functions can return values. But you cannot return a function from a function, you can only return a function pointer -- a pointer variable holding the address of the function you wish to return. All possible return values must be of the same type, therefore all function signatures and return types must be exactly the same -- only the name of the functions can differ.

What are the parts of c plus plus environment with drawing?

C++ has no built-in graphics methods. C++ is a machine-independent programming language, but graphics are machine-dependent. To make use of graphics of any kind, you must use a suitable graphics library. If you need cross-platform support, use a generic library.

How can i make my own video game what do i need to download and learn?

Figure out what you're going to make. Sort out the conceptual problems now, and you'll save yourself a lot of time in the execution.

Create a design document. Even if it's just for yourself, writing down your ideas will help you organize your thoughts and exercise your creativity. Decide what kind of game you're making, what will be needed to complete it, how the controls will work, and so on.

Above: Your concept art will probably not look like this - that's OK

Think about what's reasonable. Can you build a massive 3D world? It's certainly possible with the tools I'm about to show you, but determine whether or not it is feasible given the amount of time you're willing to dedicate. Retail games are made by teams of professionals - you're one amateur. If you're an absolute beginner, as this article assumes you are, you should probably start small and build up your game as you learn new skills.

Don't be discouraged - small doesn't mean simplistic or dull. You might assume that everything has been done before, but in actuality, gaming is still a young medium. There are billions of ideas just waiting to be had, and it doesn't matter if your game is big or small - good ideas are good ideas. Consider how quickly games went from vector graphics to 2D sprites to 3D rendering - there simply wasn't enough time to cover all the possibilities, and that fact is partially why 2D gaming is seeing a comeback.

So how are you going to build this thing? You have two options - spend some time learning a bit of scripting, or use software that requires little to none of that. It's up to you.

Eventually, if you're serious about indie game development, you'll want the flexibility offered by a full understanding of programming and computer science. Right now, however, you may just be testing the waters. No problem, there are plenty of tools for nonprogrammers. I've arranged several popular methods in roughly the order of their complexity. Some are free and others are demos.

Game Maker 7.0 - download

Game Maker is an excellent piece of free software ($20 gets you more functionality). It does what it purports to, allowing you to create relatively complex 2D games with very little knowledge of programming, though it does contain a simple scripting language for more complex operations.

RPG Maker 2003 - download

An old favorite. Unlike Game Maker, RPG Maker does limit you to one type of game, but it does RPGs very well. It's your basic tile-map, animated sprite character, random battle engine. The interface is well designed and easy-to-use, and the software includes a good array of pre-made graphics (though wouldn't you rather make your own?).

RPG Toolkit - download

While it doesn't have the most refined interface, RPG Toolkit is pretty flexible, and aids in the the creation of 2D isometric RPGs. The hard stuff has been taken care of for the most part, leaving you to work out the graphics and scripted events.

Byond - download

Byond is an interesting tool which allows you to create small online games with relative ease. You're going to have to learn a bit more in the way of coding than you would with something like GameMaker or RPGmaker, but if you absolutely must make an online game, this is probably the easiest way.

DarkBASIC- information

DarkBASIC is a version of the Basic programming language (which, as its name implies, is actually fairly easy to learn) built around DirectX. Despite being more challenging than something like Game Maker, it is possible to learn via online tutorials and create a quality game in DarkBasic.

Blender - download

Blender is a free, open source 3D modeling, animation, and game design program. Since you're dealing with 3D game creation, the learning curve is very high. For someone who isn't frustrated or deterred easily, Blender can be a great way to get into 3D game design - just be sure you're willing to spend a lot of time reading tutorials and learning through trial and error. Just because it's free doesn't mean its shoddy

But if you want to code the game yourself legit, then you might want to use one of these (or both): java, and C++ (or C, or C#). Although there are many other languages to make apps, these are most popular.

Write a program to add two 8 bit numbers in C language?

char x,y,z;

x=100;

y=200;

z=x+y; /* overflow */

x=100;

y=100;

z=x+y; /* overflow if signed, okay if unsigned */

x=100;

y=20;

z=x+y; /* okay */

10 kinds of sorting in data structure?



1.Bubble Sort
2.Insertion Sort
3.Shell Sort
4.Merge Sort
5.Heap Sort
6.Quick Sort
7.Bucket Sort
8.Radix Sort
9.Distribution Sort
10.Shuffle Sort

Do you need SQL server and c plus plus to develop Visual Studio apps?

No, you don't need VC++. VC++ is often the language of choice when programming for Windows platforms (Microsoft use it in-house for all major software developements, including Windows itself), but it is not the only version of C++ capable of producing Windows programs and is by no means the cheapest. It also comes up short with regards C++ standards compliance.

What are filestream classes in c plus plus?

The file stream classes (ifstream and ofstream) are derivatives of the I/O stream classes (istream and ostream) that are specific to file input and output.

What is the cardinality of a data type eg integer?

Cardinality is the total number of unique occurances of an entity (e.g. person, organization, or transaction) that can be included in a relationship. A URI/URL, e-mail, phone number should be unique, providing a high cardinality. This means that each of the entity values would be represented at least once in a relationship. A lower cardinality value would be something like a bit, or boolean value. This value will only occur twice before becoming redundant in a relationship. So a data type value of an integer could be either low or high, depending on what the data is used for. If the values are only 0-6, then there are only 6 occurances of the data, if the field is open to the full range of data then it would be of extremely high cardinality.

What can mouse do that keybord can not do plus?

Oh, dude, a mouse can totally click and drag, like, you know, move stuff around on the screen with ease. Meanwhile, a keyboard is just sitting there, like, typing away without any finesse. So, yeah, the mouse wins this round for being all hands-on and stuff.

Can a C plus plus user defined function return a value?

Absolutely. Indeed, any function (user-defined or built-in) that does not return a value is not really a function, it is simply a procedure.