How object take place in memory in case of oops?
Objects are stored in memory according to their members. They are very similar to struct types, insofar as the total memory assigned to an object is equal to the total size of all its members, plus any padding required for member alignment. If the object contains pointers, the memory allocated to those pointers will reside elsewhere, outside of the object, regardless of whether that memory belongs to the object or not (it is not included in the object's memory footprint). Additional memory is also set aside for the v-table if the class contains any virtual methods.
What is meant by compile time operator in c?
A compile time operator is an operator involved in an expression where the result is known at compile time. An example is the expression 1 + 2 * 4. Since 1, 2, and 4 are literal expressions, the result is known at compile time, and there is no need to generate code to evaluate it. The compiler is free to substitute the expression 9 in place of the expression 1 + 2 * 4. Don't bother doing it yourself - sometimes the clarity of writing what you are trying to accomplish makes for more well documented code.
If you do not have such a compiler, it is high time that you upgrade.
A function is a small set of instructions designed to operate on its given input (aka parameters or arguments) and perform some action or return some output. Generally in programming a commonly used strategy is to take a large program and break it into smaller chunks, which are turned into functions.
So say that you are writing a large program and constantly have to see which of two numbers is larger, you could write a function:
int larger(int a, int b)
{
if(a > b)
return a;
else
return b;
}
Now in your program, you can simply say:
int x = larger(number1, number2);
What is the importance of Swapping two variables?
Suppose we ask the user to enter a range with an upper and lower limit. We would naturally expect the upper limit to be higher in value than the lower limit. If this is not the case, we can either reject the values and ask for new input or we can simply swap them.
if we have an array of unsorted values and we wish to sort them, we need to exchange values. In order to exchange any two values we must swap them.
Why can't you use a equals equals sign for a for statement?
This answer applies to programming languages in general. The equals sign is used for assignment. For example, the code "$variable = 1" assigns the value of 1 to the variable $variable. If you want to test whether values are the same (equality) the double equals sign is used. For example, when you want to verify that $variable has been assigned the value of 1 you would use: $variable == 1
Can a derived class pointer be converted into a pointer to its public base class?
Yes, via a static up-cast. Up-casting to a base class is always safe so there's no need to dynamically cast (dynamic casting is only a requirement when down-casting, but even then that's usually a sign of a poorly-designed virtual interface).
Consider the following classes:
class A{};
class B : public class A{};
If we create an instance of B we can then point at that instance using a derived class pointer:
B b;
B* pb = &b;
To "convert" pb to a base class pointer we statically cast it like so:
A* pa = (A*) pb;
Or, more simply, we can just point at the derivative's address:
A* pa = &b;
Note that we don't actually convert anything. Although pa points to the base class, the actual instance we're pointing at is still a derived class. This is what enables us to pass pointers and references to derived classes into functions that actually expect pointers or references to base classes. Even though those functions can have no prior knowledge of what derivatives it might receive in the future, they simply don't have to. As far as the functions are concerned, you've passed a base class, not a derivative. But if you call a virtual method of the base class then the derived class method will be called instead -- and that's precisely what one expects of a derivative. This is how inheritance and polymorphism work.
If you really want to convert to a base class (completely eliminating the derived class), then you must call the base class copy constructor, passing the derived class as the argument:
B b;
A a(b);
Note that a and b are now completely separate instances and a is nothing more than the base class component of b, devoid of all its derived class components. Thus if you were to mutate a, you will not mutate b, and vice versa. The casts shown previously do not create separate instances -- there is only the one instance -- so any mutations affect all pointers to that one instance, whether they be base class pointers or derived class pointers.
You can also use the copy constructor to point at a new instance of the base class, creating the new instance on the heap rather than the stack:
A* pa = new A(b);
In order to make use of A's assignment operator, you must provide a cast operator in B:
class B : public A
{
public:
operator A(){return(A(*this));}
};
Now you can perform the following assignment:
A a;
B b;
a = b;
Note that a and b remain completely separate instances.
How do you get path of a DLL from inside the DLL itself?
http://www.codeproject.com/KB/DLL/DLLModuleFileName.aspx
Code written in a specific format to perform a single task or calculation is called a?
It's called a statement. However, C++ permits many tasks and calculations to be performed with a single statement. Good class design ensures that all the individual processes behind a statement are completely hidden from the caller, thus ensuring clean and clear code that is easily understood.
Is x plus equals y is post increment or pre increment?
The '+=' operator behaves like a pre increment operator.
Use some methods of a class in subclass without inheritance in c plus plus?
You can't have a subclass without inheritance. A subclass is just another name for a derived class, and derived classes inherit all the public and protected members of their base classes. You cannot have a base class with neither public nor protected members -- there has to be at least one exposed interface, even if it is only a static public method.
I suspect you are actually asking how to call a specific method of a derived class when all you have is a pointer to its base class. The correct solution is to use inheritance and call a virtual method of the base class. But what do you do when the specific method you want to call is non-generic?
This is a classic case of poor class design. The problem is not that you are trying to extort non-generic behaviour from a base class, the problem is in how you are going about it. If you only have a pointer or reference to the base class then you are expected to make use of its generic interface. If you need a more specific interface, then you need a pointer or reference to the more specific type, not its generic type. Unfortunately, this may not always be possible, especially when dealing with legacy code for which you have no control over. In these cases, the only solution is to resort to expensive runtime type information and dynamically cast your base class pointer to the appropriate type. If the return value is NULL then the base class does not point at your expected derivative, so you must check the return value is non-NULL before calling the specific method. This is the only safe way of doing it, in the absence of a suitable generic interface.
This, in turn, means your calling code requires knowledge of that specific type. While that's generally not a problem when the calling code is external to the base class, it is a problem when the calling code is the base class itself. This means your base class must be blessed with knowledge about some or all of its derivatives. To be blunt, that's a maintenance nightmare because every time you derive a new object for which you wish to extort specific behaviour you must also update the base class in order to accommodate that new derivative. Quite simply this is a sloppy way of going about things.
The best solution is, of course, to rethink your design. Separate your interfaces from your implementations and gather all the common interfaces together in the base class itself. Look into ways of generalising the more specific methods such that it is possible to call those methods from the generic interface, without the need for runtime type information. Use arguments as flags to allow greater flexibility, and let the more specific implementations work out for themselves exactly which calls to make based upon those arguments. You cannot avoid using inheritance, of course, but ultimately it is the correct way of doing things. Your code will be cleaner and easier to maintain in the long run. And it all starts with good design.
Write a program to swap 2 variables with out using 3rd variable through cout?
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a;
cin >> b;
a = a * b;
b = a / b;
a = a / b;
cout << a << " " << b;
char wait;
cin >> wait;
return 0;
}
What makes for a good or bad variable name in C plus plus programming languages?
A good variable name is one that is clear, related to the data it stores. Also, you should try to avoid confusions with other variables.
What is passing by address in C code?
When you pass in a memory address (ie. pointer) into a function rather than passing in the value itself.
Visual c plus plus 6.0 free installer?
Visual C++ 6.0 is more than 16 years old. It is no longer available to buy and it was never available for free. The current version is Visual C++ 2013.
Write a cpp program to convert entered date into words?
#include<iostream>
#include<sstream>
#include<chrono>
#include<ctime>
std::string months[12] { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
class date
{
public:
date (std::string = "");
date (size_t dd, size_t mm, size_t yyyy);
date (const date& dt): d{dt.d}, m{dt.m}, y{dt.y} {}
date (date&& dt): d{dt.d}, m{dt.m}, y{dt.y} {}
date& operator= (const date& dt) { d = {dt.d}, m = {dt.m}, y = {dt.y}; return *this; }
date& operator= (date&& dt) { d = {dt.d}, m = {dt.m}, y = {dt.y}; return *this; }
static bool is_leap_year (size_t year);
static date today ();
static bool validate (size_t dd, size_t mm, size_t& yyyy);
size_t day() const { return d; }
size_t month() const { return m; }
size_t year() const { return y; }
operator std::string () const;
private:
size_t d;
size_t m;
size_t y;
};
bool date::is_leap_year (size_t year)
{
if (!(year%4)) return false;
if (!(year%100)) return true;
if (!(year%400)) return false;
return true;
}
date date::today()
{
time_t tt = time (nullptr);
struct tm *tm = localtime (&tt);
return date (tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900);
}
bool date::validate (size_t dd, size_t mm, size_t& yyyy)
{
// There was no year zero!
if (!yyyy)
return false;
// There is no day zero!
if (!dd)
return false;
// Check day and month combinations.
switch (mm)
{
case 2:
if (dd > 29 (dd > 28 && !date::is_leap_year (yyyy)))
return false;
break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (dd > 31)
return false;
break;
case 4: case 6: case 9: case 11:
if (dd > 30)
return false;
break;
default:
return false;
}
// 10 dates were skipped during the switch from Julian to Gregorian
if (yyyy==1582 && mm==10 && dd>4 && dd<15)
return false;
// The date is valid!
return true;
}
date::date (size_t dd, size_t mm, size_t yyyy): d {dd}, m {mm}, y {yyyy}
{
if (!validate (dd, mm, yyyy))
throw std::range_error ("date (size_t,size_t,size_t) - invalid date values!");
}
date::date (std::string ddmmyyyy)
{
if (!ddmmyyyy.size())
{
date dd (today());
d = dd.d;
m = dd.m;
y = dd.y;
return;
}
const std::string error {"date(std::string) - invalid argument!"};
const std::string valid {"0123456789\"};
// check for invalid characters
if (ddmmyyyy.find_first_not_of (valid) != ddmmyyyy.npos)
throw std::range_error (error);
// locate first slash
size_t s1 = ddmmyyyy.find('\\');
if (!s1 s1 ddmmyyyy.npos)
throw std::range_error (error);
// ensure no more slashes
size_t s3 = ddmmyyyy.find ('\\', s2+1);
if (s3 != ddmmyyyy.npos)
throw std::range_error (error);
// parse string
std::string sd = ddmmyyyy.substr (0, s1);
std::string sm = ddmmyyyy.substr (s1+1, s2-s1-1);
std::string sy = ddmmyyyy.substr (s2+1, ddmmyyyy.size()-s2);
std::stringstream ss;
ss << sd << " " << sm << " " << sy;
size_t dd, mm, yyyy;
ss >> dd;
ss >> mm;
ss >> yyyy;
if (!validate(dd,mm,yyyy))
throw std::range_error (error);
d = dd;
m = mm;
y = yyyy;
}
date::operator std::string () const
{
std::stringstream ss;
ss << d << '\\' << m << '\\' << y;
return ss.str();
}
int main()
{
date d;
std::string input;
while (true)
{
std::cout << "Enter a date (dd\\mm\\yyyy): ";
std::cin >> input;
try
{
date t {input};
d = t;
break;
}
catch (std::range_error& e)
{
std::cerr << e.what() << std::endl;
}
}
std::cout << "Date: " << d.day();
switch (d.day() % 10)
{
case (1): std::cout << "st"; break;
case (2): std::cout << "nd"; break;
case (3): std::cout << "rd"; break;
default: std::cout << "th"; break;
}
std::cout << " " << months[d.month()-1] << ", " << d.year() << std::endl;
}
Why is it important to check the return status when opening a file in c plus plus?
The return value tells you if any errors occurred. A value of 0 typically indicates no error.
How much was CPP Limited fined?
The Card Protection Plan Limited firm was reported to have been fined about 10 million pounds. The fine was a result of the firm selling useless insurances to its clients.
Why c is called partially an OOP language?
C is not an OOP language, period. However, while C++ supports OOP it does not rely on it. With C++, you can mix procedural, structured and object-oriented principals by mixing C++ code with C-style code and even raw assembly routines, neither of which are object-oriented.
If an exception is not caught then your program has undefined behaviour. Ultimately the program crashes, but since you haven't handled the exception you've no way of knowing what damage has been done. Files could be wiped or currupted, planes could fall from the sky... anything is possible with undefined behavour.
The best way to deal with unknown exceptions is to first catch them with a catch-all. However, you cannot actually handle the exception unless you know what type of exception you are actually dealing with. Thus the normal course of action is to assume the worst, perform any and all necessary cleanup, log the exception as an unknown exception and rethrow. As the exception percolates back down the call stack, all other exception handlers should do the same: cleanup, log and rethrow. If you're lucky, another handler might recognise the exception and be able to provide more detailed information on the type of exception. Ultimately you must never allow a program to continue executing if you cannot handle an exception.
C++11 offers a more elegant solution using a nested try catch within a catch-all. However, it makes more sense to place the nested try catch in a global function (e.g., handle_eptr()) which can specifically deal with all catch-all exceptions. Like so...
#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>
void handle_eptr(std::exception_ptr eptr)
{
try
{
if (eptr) std::rethrow_exception (eptr);
}
catch (const std::exception& e)
{
std::cerr << "Exception: "" << e.what() << ""\n";
}
}
int main()
{
try
{
std::string().at(1); // throws "invalid string position"
}
catch(...) // catch-all
{
// perform any necessary cleanup here before calling the global handler
handle_eptr (std::current_exception());
}
}
Output:
Exception: "invalid string position"
Note that handle_eptr() receives a std::exception_ptr. By itself this is useless, but std::rethrow_exception() converts the eptr to a standard exception which can then be thrown, caught and logged. You must still perform any necessary cleanup and allow the program to terminate gracefully, but at least you now know what type of exception you are dealing with and can provide a specific handler to handle it.
Note that handle_eptr() should be fleshed out to accept all necessary debug information such as the filename and the line number from the catch_all that called it. The example merely demonstrates how to pass the eptr and convert it into an actual exception.
What is the difference between implicit and explicit call of constructor in c plus plus?
An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.