What is the program to create a string class which stores a string value in c plus plus?
C++ already provides a string class in the C++ standard template library.
#include<iostream>
#include<string>
int main()
{
using namespace std;
string s {"Hello world!"};
cout << s << endl;
}
These are two separate commands. File-New creates a new file although in an IDE you must select the type of file you wish to create. CTRL+S saves the currently active file.
How do you assign or print special symbols in c plus plus?
UTF-16 strings or characters (std::wstring or wchar_t) are the best method of assigning and printing special symbols. UTF-8 encoding using std::string can be used to minimise memory consumption but still requires conversion to wide-string for printing purposes. However, if the symbols are within the range of extended ASCII character codes (0x00 to 0xff), then an unsigned char or std::string is all you really need.
Can two different variables in the same program may have the same name?
Yes, so long as they are declared in separate scopes. But just because it CAN be done, it doesn't mean it SHOULD be done. When you need to access both variables within the same scope, you need to have some way of differentiating them. You could use namespaces to explicitly declare their scope, but your code will be that much easier to read if you simply give each variable an unique name to begin with. This could be as simple as prefixing variables with "g_" for global scope, "f_" for file scope and "m_" for class scope. That way, local function variables don't require prefixes.
The following example demonstrates redeclaration of variables x and y within the same function (within nested scopes), thus making it impossible to access the previous declarations until the new declarations fall from scope. While this can be useful to protect the original variables, there are far better, less ambiguous ways of achieving this: consistent use of the const keyword along with unique identifiers; or simply passing the values (by value) to another function altogether. Ultimately, the fewer ambiguities in your code the easier it will be to read and maintain.
void foo( int x )
{
printf( "x (function level) = %d\r\n", x );
int y = x;
printf( "y (function level) = %d\r\n", y );
if( y )
{
int x=y; // new declaration -- protects previous declaration.
++x;
printf( "x (code block 1) = %d\r\n", x );
int y=x; // new declaration -- protects previous declaration.
printf( "y (code block 1) = %d\r\n", y );
if( y )
{
int x=y; // new declaration -- protects previous declaration.
++x;
printf( "x (code block 2) = %d\r\n", x );
}
printf( "x (code block 1) = %d\r\n", x );
}
printf( "x (function level) = %d\r\n", x );
printf( "y (function level) = %d\r\n", y );
}
int main()
{
foo( 1 );
return( 0 );
}
Does c plus plus need a function named main?
If this is a homework assignment, you really should try to answer it on your own first, otherwise the value of the reinforcement of the lesson due to actually doing the assignment will be lost on you.
Every C or C++ program (but see below) needs one and only function named main. The standard prototype is int main (int argc, char *argv[]); The run-time library looks for main as its first function to call after startup initialization.
In a Microsoft Windows application, the entry point is winmain. Operation is similar, though parameters are different.
Can you give an example of a basic 'Hello World' program?
#include<iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
return(0);
}
How many STD are transferred from prostitution?
HIV,AIDS,HERPES,CHLAMYDIA,HEPATITIS B,SYPHILIS,GONORRHEA,etc
What is the notation used to place block of statements in a looping structure in C plus plus?
for( ; ; )
{
statement_block;
}
while( conditional_expression )
{
statement_block;
}
do
{
statement_block;
}while( conditional_expression )
What is Conditional Compilation in C plus plus Any simple example?
Conditional compilation is used to exclude code segments from specific builds. For instance, in debug mode you might include additional code such as an assertion macro which you wouldn't really want in the release code.
The following example declares and defines an ASSERT() macro when the program is compiled in debug mode (when DEBUG is defined). In non-debug mode (when DEBUG is not defined), all calls to ASSERT() will be ignored during compilation because there is no implementation provided.
#ifdef DEBUG
#define ASSERT(x) \
if(!(x)) \
{ \
std::cout << "ERROR!! Assert " << #x << " failed\n"; \
std::cout << "on line " << __LINE__ << std::endl; \
std::cout << "in file " << __FILE__ << std::endl; \
}
#else
#define ASSERT( x )
#endif _DEBUG
Within your code you can call ASSERT() to ensure your invariants are true whilst in debug mode:
int x=1, y=1;
ASSERT( x == y );
// .. remainder of code...
In debug mode, the compiler expands the code for you, just as if you'd written the following:
int x=1, y=1;
if(!( x==y ))
{
std::cout << "ERROR!! Assert " << #x << " failed\n";
std::cout << "on line " << __LINE__ << std::endl;
std::cout << "in file " << __FILE__ << std::endl;
}
// .. remainder of code...
But in release mode there is no macro to expand, so the assert is effectively ignored:
int x=1; y=1
// .. remainder of code...
Other uses of conditional compilation include catering for UNICODE or MCBS encoding of strings. If your program uses one or the other, you can use macros to ensure the appropriate string handling calls and declarations are made.
They are also used to "guard" include files to ensure an included file is never included more than once during compilation. This is important when several files include the same file for the purpose of syntax checking. During compilation, however, only one copy of the file needs to be physically included. If multiple inclusions were not guarded against, the compiler would try to include several declarations of the same functions and, even though they all come from the exact same file, the compiler treats them as separate entities and immediately stops the compilation. The same thing occurs when a function is declared twice in two separate files.
// myclass.h
#ifndef _myclass_h_ // First time around, this will not be defined.
#define _myclass_h_ // Now it is defined.
// Declarations (with or without implementations) go here.
#endif _myclass_h_ // Marks the end of the inclusion.
When the compiler "sees" this file for the first time, it will be included because _myclass_h_ would be undefined first time around. But all subsequent inclusions will be ignored because _myclass_h_ was defined during the first inclusion.
What is a primitive type variable in c plus plus?
same the types used in C. that is int...char...float...
How do you retrieve and display system time in VC plus plus?
#include<iostream>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
int main()
{
_tzset();
char buff[128];
_strtime_s (buff, 128);
std::cout << "\Time: " << buff[0];
}
What is the header file for using graphics in c plus plus?
There is no graphic.h in the standard C++ language. It typically ships with 3rd party C++ implementations that incorporate the Borland Graphic Interface or one of its variants, such as Embarcadero Builder. It can also be used with Dev C++ if you install the WinBGIM library. Its primary purpose is to provide Windows graphics support since C++ has no built-in graphics support of any kind.
Where can i get code for C plus plus airline program?
Not to be blunt, but if you want code for an "airline program", then you have to actually sit down and write it yourself. What constitutes an "airline program"? Is it a flight simulator or a business simulator? Either way, nobody is simply going to give you the code for a non-trivial application such as this.
To write one yourself, first think about all the types you will need. Given it is an airline you will obviously need an "aeroplane" base class from which all types of aircraft will be derived. Everything that is common to all aircraft (both attributes and interface) should be encapsulated by your base class, with specific implementation details handled by your specific derivatives. You may also find it easier if you break your abstract aeroplane down in to separate components (landing gear, flight controls, engines, fuel, and so on), thus making it possible to construct specific aeroplanes from specific components.
You must also consider the "world" object; the arena in which your simulation is set, which will naturally include airports. If it is a business simulation, you must also consider passengers, cargo, ground crews -- and competitors! Each of these types must be modelled separately using individual classes, both abstract and concrete. Once you have all your types defined, concentrate on the algorithms that will allow these types to interact in meaningful ways. Identify common features and use templates as much as possible to keep code duplication to a minimum.
With your types and algorithms in place you can then concentrate on the graphical side of things; how to present these types to the end-user. Finally, you can provide the "script" that brings everything together.
Clearly this is not a project you will complete overnight; it may take years all by yourself. But only by getting your hands dirty with such a project will you appreciate just how much effort actually goes into writing non-trivial software and why no-one is simply going to give you the code.
There are plenty of programming forums that will provide free help if you get stuck, but you have to actually make some effort first. Nobody is going to do your homework for you. You're not in school anymore.
1. What do you mean by "math generally"? Do you mean algebra, pre-algebra, calculus, statiscs, trigonometry, arithmetic, or other?
2. That depends. I mean, if you want to make a web browser, you don't have to know that much about math if you just make a basic one. But if you want to make a more advanced web browser, you will have to.(Trust me, e.g. testing kbps, packets/s, IP connections and branches, etc.) But if you want to make something like a 3D game, that will require tons of math, like trigonometry(sin, cos, tan will be very useful), and especially matrices. (Shifting matrices and converting them into pixels are like the most important thing you need to know in that case, and matrices are pretty easy in math.) So yeah, I guess if you want to unlock more chances of making a really good program, you're better off first taking a few weeks extending your mathematical knowledge!
How do you animate Tom and Jerry in C programming?
Generally you wouldn't. Tom and Jerry are owned by Time Warner and only they have the legal right to animate the characters or allow others to do so on their behalf. Even so, they wouldn't use C programming to do so, they'd use animation software, as would anyone producing animations of any kind. The software may well be written in C/C++, but this is not the same as animating in C/C++. At the core of all animation software is a 2D or 3D engine that provides the basics of animation; multi-level 2D backgrounds and foregrounds or a 3D virtual world -- the "stage" within which to animate the "actors", which are modelled separately and composited within the "stage", along with "props". The output of all animation software is a movie file (AVI, MPEG, etc) in standard or high definition, or a series of still images (BMP, etc) that can be replayed as a movie.
Program for inserting a new node in between two list in c?
To insert a new node between two lists, append the new node to the first list, then insert the head node of the second list after the new node.
In c plus plus Programming can you replace STDio header with using name space STD?
No. You can't use namespace std even if you include stdio.h. At the very least you must include stddef.h before you can use namespace std.
What can you do to an int that you cannot do to a string in C plus plus?
You can perform arithmetic with it.
int x {42};
x *= 2; // ok
std::string s {"Hello"};
s *= 2; // error: std::string::operator*= (int) is undefined
What does the error 'Null Pointer Assignment' mean?
The null pointer assignment error means your program has attempted to access a memory address that does not belong to your program. This typically occurs when accessing memory indirectly through a pointer:
int* p = nullptr; *p = 42; // Error: null pointer assignment
The above is the classic example of this type of error. The null address is typically the all-zeroes address (0x0) but, regardless of the physical address, it must never be accessed because it is a system address. We typically refer pointers to the null address when they are no longer in use or we don't have an address we can (yet) assign to them.
Passing unchecked pointers to functions is another common cause:
void f (int* p) {
*p = 42; // potential error
// ...
}
In the above example there's no guarantee p refers to a non-system address. Although we can easily test p is non-null before accessing it, that won't guarantee p refers to a non-system address. However, we can greatly reduce the risk of error by passing memory address via references instead of pointers:
void f (int& r) {
r = 42;
// ...
}
There's still potential that r refers to a system address if the address were passed via a pointer, however there is seldom any need to use unchecked pointer variables in C++. References and resource handles (or smart pointers) eliminate the need for pointers and are actually more efficient than pointers because testing for null becomes largely redundant.
The only time we really need a pointer is when "no object" is a valid argument:
void f (int* p) {
if (p == nullptr) {
// the "no object" code
} else {
// code that operates on an object
}
}
The following example sets up a two-dimensional array, initialises it with some pseudo-random data, and then prints the table and the averages.
#include<iostream>
#include<time.h>
int main()
{
const int max_students = 7;
const int max_student_grades = 5;
const int max_grades = 6;
const char grade[max_grades]={'A','B','C','D','E','F'};
srand((unsigned) time(NULL));
// Initialise the array with pseudo-random grades:
int table[max_students][max_student_grades];
for(int student=0; student<max_students; ++student)
{
for(int student_grade=0; student_grade<max_student_grades; ++student_grade)
{
table[student][student_grade] = rand()%max_grades;
}
}
// Print the table and average the results.
int overall=0;
for(int student=0; student<max_students; ++student)
{
int average=0;
std::cout<<"Student #"<<student+1;
for(int student_grade=0; student_grade<max_student_grades; ++student_grade)
{
std::cout<<" Grade #"<<student_grade+1<<": "<<grade[table[student][student_grade]]<<", ";
average+=table[student][student_grade];
}
std::cout<<" Average: "<<grade[average/max_grades]<<std::endl;
overall+=average;
}
std::cout<<"Overall average: "<<grade[overall/max_grades/max_students]<<std::endl;
return(0);
}
Example output:
Student #1 Grade #1: A, Grade #2: E, Grade #3: D, Grade #4: E, Grade #5: F, Average: C
Student #2 Grade #1: E, Grade #2: D, Grade #3: E, Grade #4: E, Grade #5: E, Average: D
Student #3 Grade #1: D, Grade #2: A, Grade #3: D, Grade #4: B, Grade #5: A, Average: B
Student #4 Grade #1: C, Grade #2: B, Grade #3: A, Grade #4: A, Grade #5: B, Average: A
Student #5 Grade #1: E, Grade #2: D, Grade #3: C, Grade #4: F, Grade #5: E, Average: D
Student #6 Grade #1: C, Grade #2: D, Grade #3: A, Grade #4: F, Grade #5: A, Average: B
Student #7 Grade #1: B, Grade #2: D, Grade #3: F, Grade #4: B, Grade #5: C, Average: C
Overall average: C
What is a plus b plus c plus d plus e to the power of 2?
a2+2ab+b2+2ac+2bc+c2+2ad+2ae+2bd+2be+2cd+2ce+d2+2de+e2
What is the C plus plus Algorithm for Knapsack Problem?
#include<iostream>
#include<vector>
#include<cassert>
template<typename _Ty>
class subset
{
public:
using value_type = typename _Ty::const_iterator;
using container_type = std::vector<value_type>;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
using reverse_iterator = typename container_type::reverse_iterator;
using const_reverse_iterator = typename container_type::const_reverse_iterator;
using size_type = typename container_type::size_type;
~subset (void) {};
explicit subset (const _Ty&);
subset (const subset&) = delete;
subset (subset&&) = delete;
subset& operator= (const subset&) = delete;
subset& operator= (subset&&) = delete;
const container_type& first (size_type);
const container_type& next (void);
private:
size_type m_size; // desired size of the subset
container_type m_set; // iterators to the full set
container_type m_subset; // subset of the full set of iterators
const_iterator find (const value_type&) const;
const bool get_next (const bool = true);
};
template<typename _Ty>
inline subset<_Ty>::subset (const _Ty& set):
m_size {set.size()}, m_set {}, m_subset {}
{
for (value_type it=set.begin(); it!=set.end(); ++it)
m_set.push_back (it);
m_set.shrink_to_fit();
}
template<typename _Ty>
inline typename subset<_Ty>::const_iterator subset<_Ty>::find (
const value_type& value) const
{
const_iterator it=m_set.begin();
while (it!=m_set.end() && *it!=value) ++it;
return it;
}
template<typename _Ty>
inline const typename subset<_Ty>::container_type& subset<_Ty>::first (
size_type count)
{
assert (count <= m_set.size());
m_size = count;
m_subset.clear();
for (const_iterator it=m_set.begin(); count--; ++it)
m_subset.push_back (*it);
return m_subset;
}
template<typename _Ty>
inline const bool subset<_Ty>::get_next (
const bool pop /* = true */)
{
const_reverse_iterator sub_it = m_subset.rbegin();
const_iterator set_it = find (*sub_it);
if (pop)
m_subset.pop_back();
if (++set_it != m_set.end())
m_subset.push_back (*set_it);
return m_subset.size() 1U)
return m_subset;
const value_type value = m_set.back();
m_set.pop_back();
--m_size;
next();
m_set.push_back (value);
if (m_size++ != m_subset.size())
return m_subset;
get_next (false);
return m_subset;
}
struct item
{
size_t value {0};
size_t weight {0};
item (size_t v=0, size_t w=0): value{v}, weight{w} {}
};
using vector = std::vector<item>;
item sum (vector& v)
{
item result;
for( auto i : v)
{
result.weight += i.weight;
result.value += i.value;
}
return result;
}
vector knapsack (vector items, size_t capacity)
{
vector v;
subset<vector> set {items};
for (size_t n=items.size(); n; --n)
{
subset<vector>::container_type selection {set.first (n)};
for (; selection.size(); selection=set.next())
{
vector t;
for (auto i : selection)
{
t.push_back (*i);
}
item s = sum (t);
if (s.weight <= capacity && sum(v).value < s.value)
v = t;
}
}
return v;
}
int main()
{
size_t capacity {4};
vector items {{1,8},{2,4},{3,1},{2,5},{2,3}};
vector rucksack = knapsack (items, capacity);
std::cout << "Items:\n" << std::endl;
for (auto i : items)
std::cout << '£' << i.value << '\t' << i.weight << "kg" << std::endl;
std::cout << std::endl;
std::cout << "Optimal selection for capacity " << capacity << "kg\n" << std::endl;
for (auto r : rucksack)
std::cout << '£' << r.value << '\t' << r.weight << "kg" << std::endl;
std::cout << std::endl;
item s = sum (rucksack);
std::cout << "Total value and weight:\n" << std::endl;
std::cout << '£' << s.value << '\t' << s.weight << "kg" << std::endl;
std::cout << std::endl;
}
How do you write sum of two hexadecimal numbers in c plus plus?
#include<iostream>
#include<queue>
#include<stack>
int main()
{
int x = 0x1f;
int y = 0xa2;
std::cout.hex;
std::cout << "0x" << std::hex << x <<
" + " << "0x" << std::hex << y <<
" = " << "0x" << std::hex << x+y <<
std::endl;
}
How do you define a constructor function for a Date class that initializes the Date objects?
The following is one possible method. If the input arguments would result in an invalid date (or no arguments are given), then the members are set to an arbitrary default date (in this case 1/1/1980).
An alternative method that ensures all dates are valid (non-default) would be to throw an exception from the set() member. If your code is producing invalid date arguments (causing the exception to be thrown) then you really need to fix the code that generates those dates. Note that the static methods, is_valid() and is_leap_year(), can be used to check date arguments without actually constructing a date object.
Another alternative would be to set a flag that denotes if the date object is valid or not. However, this is the least intuitive method as the onus is then placed upon the user to always check the flag before using the object.
#include<iostream>
#include<iomanip>
class cdate
{
unsigned m_day;
unsigned m_month;
unsigned m_year;
public:
cdate (unsigned day, unsigned month, unsigned year): m_day(1), m_month(1), m_year(1980) { set (day, month, year); }
cdate (const cdate& date): m_day(date.m_day), m_month(date.m_month), m_year(date.m_year) {}
cdate& operator= (const cdate& date) { m_day=date.m_day; m_month=date.m_month; m_year=date.m_year; }
bool set (unsigned day, unsigned month, unsigned year);
static bool is_valid (unsigned day, unsigned month, unsigned year);
static bool is_leap_year (unsigned year);
unsigned day() const { return m_day; }
unsigned month() const { return m_month; }
unsigned year() const { return m_year; }
};
bool cdate::is_valid (unsigned day, unsigned month, unsigned year)
{
// handle zeroes
if (!day !month !year)
return false;
// handle invalid month
if (month>12)
return false;
// handle 31 day months
if ((month==1 month==3 month==5 month==7 month==8 month==10 month==12) && day>31)
return false;
// handle 30 day months
if ((month==4 month==6 month==9 month==11) && day>30)
return false;
// handle leap years
if (month==2)
{
if (!is_leap_year (year) && day>28)
return false;
else if (day>29)
return false;
}
// data ok
return true;
}
bool cdate::is_leap_year(unsigned year)
{
// common year (not divisible by 4)
if (year%4)
return false;
// leap year (not divisible by 100)
else if (year%100)
return true;
// leap year (divisible by 400)
else if (year%400==0)
return true;
// common year
return false;
}
bool cdate::set (unsigned day, unsigned month, unsigned year)
{
if (is_valid (day, month, year))
{
m_day = day;
m_month = month;
m_year = year;
return true;
}
// alternatively, throw an exception here
return false;
}
std::ostream& operator<< (std::ostream& os, const cdate& date)
{
os
<< std::setw (2) << std::setfill('0') << date.day() << '/'
<< std::setw (2) << std::setfill('0') << date.month() << '/'
<< std::setw (4) << std::setfill('0') << date.year();
return os;
}
int main ()
{
// invalid date (was not a leap year)
std::cout << "Input:\t29/02/1900" << std::endl;
cdate d1(29,2,1900);
std::cout << "Output:\t" << d1 << std::endl;
// valid date (was a leap year)
std::cout << "Input:\t29/02/2000" << std::endl;
cdate d2(29,2,2000);
std::cout << "Output:\t" << d2 << std::endl;
}