What is derived class in brief?
A derived class is any class that inherits from one or more other classes, known as base classes. The derived class inherits the sum total of all public and protected members of all its base classes, including their base classes. The derived class is a more specialised form of its base classes. Any members of the base classes that are declared virtual can be overridden, such that calling the base class method directly actually invokes the derived class method, thus enabling polymorphic behaviour.
How do you write a C plus plus program to concatenate any two strings using operator overloading?
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class Add{
public:
char *rep;
Add(){}
Add(char *tem){
rep = new char[strlen(tem)+1] ;
strcpy(rep,tem);
}
Add operator + (const Add &rhs)
{
char *temp;
temp= new char[strlen(rep) + strlen(rhs.rep)+1];
temp = strcat(rep,rhs.rep);
return Add(temp);
}
};
int main()
{
cout<<"hello";
Add obj1("pradeep");
Add obj2("bansal");
Add obj3;
obj3 = obj1+obj2;
cout<<obj3.rep;
getch();
return 0;
}
Example of predefined function?
function callMe (fx_params) {
alert(fx_params);
}
callMe('Alert Loaded from a JavaScript function');
This would be in a javascript page, or in a JS Script tag..
- Caleb
Convina Web Design and Hosting
Can a friend function of derived class access private data of base class?
In some computer languages it is possible to do so, but I would not even think or design any application in this way.
A base class SHOULD NEVER know what the derived classes are. Perhaps it was created by generalizing some classes. Even at that point, this new base class should have no knowledge of the derived classes whatsoever.
To do a good OO design, the base class should have a method like getPrivatePartOfDerivedClass() as abstract, then force the derived class to provide the implementation of this method.
Why constructor does not have return type?
Constructors cannot have return types. The main job of the constructor is to create new instance of its class and return that instance. So the default return type of all constructor is the object of its class.
Enumerations are a method of grouping constant values. For example:
enum suits { clubs, diamonds, spades, hearts };
By default, the first constant is assigned the value 0 and all subsequent values increment by 1. However, you can assign any value to any constant -- the automatic increments will continue from that point.
enum suits { clubs = 1, diamonds, spades, hearts };
You can also assign the same value to multiple constants.
enum suits { clubs = 1, diamonds, spades = 1, hearts };
By grouping constants within enumerations your code becomes more secure because you cannot pass constant literals into functions that expect an enumeration. Consider the following:
void print_suit(unsigned id)
{
switch (id)
{
case (0): std:cout << "Clubs"; break;
case (1): std:cout << "Diamonds"; break;
case (2): std:cout << "Spades"; break;
case (3): std:cout << "Hearts"; break;
default: std::cout << "Invalid";
}
}
In the above example there is nothing to prevent the caller from passing an invalid value, such as 42, which the function caters for with a default case. However, by passing an enum instead, invalid values are completely eliminated:
void print_suit(suits suit)
{
switch (suit)
{
case (clubs): std:cout << "Clubs"; break;
case (diamonds): std:cout << "Diamonds"; break;
case (spades): std:cout << "Spades"; break;
case (hearts): std:cout << "Hearts";
}
}
What does the use namespace STD syntax do?
The std namespace is the standard library, which includes many of the common data types, constants, structures, classes and functions that you will use to create C++ programs. There are very few non-trivial C++ programs that do no make use of at least some portion of the standard library at some point. Note that you need only include those portions you actually use; there is no need to include the entire standard library. Any built-in functions that require the standard library will include only as much as they need to, whether you yourself include those portions or not.
Write a program in c plus plus to sort a book list in library?
#include<iostream>
#include<string>
#include<vector>
struct book
{
std::string title;
std::string author;
// other members ...
};
int main()
{
std::vector<book> books;
books.push_back ( book {"Moby Dick", "Herman Melville"});
// ...
}
Parameterized constructor in c plus plus?
Every class requires at least one constructor, the copy constructor. It is implied if not declared. If no constructors are declared, a default constructor is also implied. Every class also requires a destructor, which is also implied if not declared.
The purpose of constructors is to construct the object (obviously) but by defining your own you can control how the object is constructed, and how member variables are initialised. By overloading constructors, you allow instances of your object to be constructed in several different ways.
The copy constructor's default implementation performs a member-wise copy (a shallow-copy) of all the class members. If your class includes pointers, you will invariably need to provide your own copy constructor to ensure that memory is deep-copied. That is, you'll want to copy the memory being pointed at, not the pointers themselves (otherwise all copies end up pointing to the same memory, which could spell disaster when one of those instances is destroyed).
The destructor allows you to tear-down your class in a controlled manner, including cleaning up any memory allocated to it. If your class includes pointers to allocated memory, you must remember to delete those pointers during destruction. The destructor is your last-chance to do so before the memory "leaks". The implied destructor will not do this for you -- you must implement one yourself.
class foo
{
public:
foo(){} // Default constructor.
foo(const foo & f){} // Copy constructor.
~foo(){} // Destructor.
};
Swap function in c plus language?
#include<iostream>
void swap(int* x, int* y)
{
int tmp = *x; *x=*y; *y=tmp;
}
int main()
{
int a=2, b=4;
std::cout<<"a="<<a<<", b="<<b<<std::endl;
swap(&a, &b);
std::cout<<"a="<<a<<", b="<<b<<std::endl;
}
How do you write the concatenate the strings using pointer?
char* strcat (char* destination, const char* source) {
char* return_value = destination; // temp destination for return
while (*(destination++) != '\0'); // find end of initial destination
while ((*(destination++) = *(source++)) != '\0'); // copy source to end of destination
return return_value; // return original value of destination
}
Do you use virtual functions in Java?
Every method in java that isn't a class (static) method is automatically "virtual." If you want to disable virtual overrides, make the method "final" in the base class.
How is a c plus plus program stored in the memory?
C++ programs are not stored in memory (RAM) they are stored on mass storage devices (usually disk drives). When compiled, they produce machine code programs which contain machine instructions and their operands. These are also stored on mass storage devices, but when loaded into memory the machine instructions are executed by the CPU.
What is the difference between struct in c and c plus plus?
In C, a struct is simply a type that can hold several sub-objects.
In C++, struct is almost the same as "class". It can have member functions, parent structs and classes, etc. The only difference between struct and class is that the members of class are by default private, while the members of struct are by default public. Thus, a standard C struct is also a good C++ struct - simply one that has no member functions and no parents.
How can you explain reverse polish notation in simple words?
Reverse Polish Notation (RPN) is a system where expressions are evaluated from left to right, with no precedence, with operators stated following the operands. A typical RPN expression might be ab+c*, which in infix means (a+b)*c. (Infix is our "standard" system which we use when writing programs) Note that the infix expression required parenthesis to override operator precedence, but the RPN expression did not. If you said in infix a+b*c, the RPN equivalent would be abc*+. The value of RPN is that it is easy to build a stack machine that processes the expression. Each time you encounter an operand, you push it on the stack. Each time you encounter an operator, you process it, replacing the top two elements of the stack with the result (for binary operators), or replacing the top element of the stack with the result (for unary operators). RPN is often used in compiler design.
Can somebody give me the program to find simple interest using friend function in c plus plus?
Here is an example program:
class obj{
public:
float p,n,r,si;
friend void calc( obj temp);
};
void calc( obj temp){
si = (p*n*r)/100;
}
The initialization and function calling is essential.
What is a template class in c plus plus?
Templates allow us to reduce the need to write duplicate code ourselves and force the compiler to duplicate the code for us. This saves a great deal of time (and money) and greatly reduces code maintenance because there's only one version of the implementation to modify. This greatly reduces the chances of us making a mistake because the compiler will propagate all the changes for us. Moreover, we don't need to second-guess what data types we might need to cater for in the future because the compiler will generate specific versions on an as-required basis, according to the types we actually use -- including those we haven't yet designed!
You've probably been taught that code duplication is a bad thing -- and you'd be right -- but it is also a necessary evil. The difference with templates is that rather than placing the onus upon the programmer to ensure the code duplication is consistent, the onus is placed entirely upon the compiler.
So when do we need code duplication? We need it whenever the implementation of a function or a class differs only in the type of data they operate upon. That is, one class or function definition that caters for any data type, rather than creating many separate classes or functions for each data type. If the implementations are exactly the same in every way, then duplicate code is inevitable. It's simply a matter of whether we write it ourselves and accept all the pitfalls that come with that, or we let the compiler do all the donkey work for us. The latter is always the preferred option every time.
Consider the following simple example:
int max(int a, int b){ return(a
double max(double a, double& b){ return(a
foo& max(foo& a, foo& b){ return(a
As you can see, all three functions have exactly the same implementations, they only differ in the type of data they operate upon. This is where templates come in. If we replace the data type with a template parameter then we need only define the function once:
template
T max(T a, T b){ return(a
Note that T is a standard token for a template parameter. If we needed more than one data type in the same function or class then we'd use tokens T1, T2, etc. The compiler will replace these tokens with the actual data type we supply at compile time.
Now when we call max(), the compiler will generate the appropriate overload for us at compile time, provided both arguments are covariant (are of the same intrinsic type). Thus the following are acceptable calls to max():
int x=42;
int y=2112;
int z=max(x,y); // z==2112
std::string s1("hello");
std::string s2("world");
std::string s3=max(s1,s2); // s3=="world"
It should be noted that since templates are incomplete types, the implementation must be made visible to the compiler BEFORE encountering any usage of the template for the first time. Thus you must either define the function within the declaration, or outside of the declaration but within the same file or #include the file containing the definition immediately after the declaration. If defining outside of the declaration, remember to also include the template:
// declaration...
template
T max(T a, T b);
// definition...
template
T max(T a, T b) { return(a
The same principal applies to classes where one or more data members employ template parameters. However, you must include the template keyword in every member method defined outside of the class, including the class constructors, as per the following example:
template
class foo
{
public:
foo(const T data):m_data(data){}
foo(const foo&); // defined externally...
private:
T m_data;
};
template
foo
Whenever you instantiate any objects from a template class, you must also provide the data type(s) so the compiler knows how to generate the complete class definition. For example:
foo
foo
Now that you have a basic understanding of templates, you should find working with the STL containers (including std::list and std::vector) that much easier.
A template is an incomplete type. Template functions can be likened to overloaded functions where the implementation is exactly the same, the only difference being the type of argument or arguments passed to the function. For instance, when determining the larger of two objects of the same type, the implementations would be the same regardless of the type, like so:
int& max(int& x, int& y){ return(y float& max(float& x, float& y){return(y Since this function can be applied to any object that supports the less-than operator, writing all the overloads by hand places a heavy burden upon the programmer. Every time the programmer creates a new object that supports the less-than operator, they would have to write a new overload to cater for it. C++ templates make it much simpler by allowing the programmer to define the function just once, using template parameters instead of formal arguments, like so: template T& max(T& x, T& y){ return(y When you call the max function, the compiler automatically generates the appropriate overload for you (unless one was already generated by an earlier call with the same type). Function templates can also be used within classes (template methods). In this case, the compiler generates separate overloads for each method within the class according to the types passed to those methods. However, if the class contains a template data member, the compiler generates separate classes for each type. This is known as a template class. The following code demonstrates all these concepts: #include // template function: template T& max(T& x, T& y){ return( y // template method: struct A { template T& max(T& x, T& y){ return( y }; // template class: template struct B { T data; const bool operator< (const B& b){ return( this->data }; template std::ostream& operator<< (std::ostream& os, const B int main() { int i1=24, i2=42; double d1=4.2, d2=2.4; A a; B B B B std::cout<<"Calling template function:\n"< std::cout<<"The maximum of "< std::cout<<"The maximum of "< std::cout< std::cout<<"Calling template method of class A:\n"< std::cout<<"The maximum of "< std::cout<<"The maximum of "< std::cout< std::cout<<"Calling template function with template class B types:\n"< std::cout<<"The maximum of "< std::cout<<"The maximum of "< std::cout< std::cout<<"Calling template method of class A with template class B types:\n"< std::cout<<"The maximum of "< std::cout<<"The maximum of "< std::cout< } Output Calling template function: The maximum of 24 and 42 is 42 The maximum of 4.2 and 2.4 is 4.2 Calling template method of class A: The maximum of 24 and 42 is 42 The maximum of 4.2 and 2.4 is 4.2 Calling template function with template class B types: The maximum of 24 and 42 is 42 The maximum of 4.2 and 2.4 is 4.2 Calling template method of class A with template class B types: The maximum of 24 and 42 is 42 The maximum of 4.2 and 2.4 is 4.2 Note that there is only one class A type, and that it contains two overloads of the max function. However there are two class B types (one for int, the other for double). Thus although you could assign bi1 to bi2 because they are the same type, you cannot assign bi1 to bd1 because they are different types.
Can you delete Microsoft visual c plus?
To delete Microsoft Visual C/C++, like any other Windows program, simply uninstall it using Control Panel / Add-Remove Programs. For additional information, consult the readme that came with the original program.
What is the program structure of C language and C plus plus?
There is no single structure to a C++ program. C++ is multi-paradigm and allows programmers to use any combination of C-style programming, object-oriented programming and template metaprogramming using a mixture of primitive built-in types, standard library types and user-defined types.
C plus plus Program that will convert from decimal to octal?
The following code will convert any number in any base to any other base, from binary to hexadecimal, and everything inbetween.
#include<iostream>
#include<string>
#include<sstream>
typedef unsigned long long ull;
typedef unsigned long ul;
const std::string symbols="0123456789abcdef";
std::string inputvalue(ul base)
{
using namespace std;
string value;
while(1)
{
cout<<"Enter a positive value : ";
string s;
getline(cin,s);
if(s.size())
{
for(string::iterator i=s.begin();i!=s.end();++i)
if(*i>='A' && *i<='Z')
*i+=32;
string actual = symbols.substr(0,base);
if(s.find_first_not_of(actual)!=string::npos)
{
cout<<"The value you entered is invalid for the base.\n"
<<"Please enter another value.\n"<<endl;
continue;
}
value=s;
break;
}
}
return(value);
}
ul inputbase(std::string prompt)
{
using namespace std;
ul result=0, min=2, max=16;
while(1)
{
cout<<prompt.c_str()<<" ["<<min<<".."<<max<<"] : ";
string s;
getline(cin,s);
if(s.size())
result=stoul(s,0,10);
if(result<min result>max)
cout<<"The base must be in the range "
<<min<<".."<<max<<"\n"
<<"Please enter another base.\n"<<endl;
else
break;
}
return(result);
}
ull base2dec(std::string value,ul base)
{
ull col=1, num=0;
for(std::string::reverse_iterator i=value.rbegin(); i!=value.rend(); ++i)
{
num+=symbols.find(*i,0)*col;
col*=base;
}
return(num);
}
std::string dec2base(ull dec,ul base)
{
using namespace std;
int len=1;
ull tmp=dec;
while(tmp/=base)
++len;
string value("0",len);
while(dec)
{
value[--len]=symbols[dec%base];
dec/=base;
}
return(value);
}
int main()
{
using namespace std;
ul base=inputbase("Enter the base of the value");
string value=inputvalue(base);
ul newbase=inputbase("Enter the base to convert to");
value=dec2base(base2dec(value,base),newbase);
cout<<"New value:\t"<<value.c_str()<<endl;
return(0);
}
What is a dosh header file in c?
Not sure what dosh is but dos.h was used by Turbo C/C++ to handle DOS interrupts way back in the early 90s. DOS has largely been consigned to the history books now that Windows is an OS in its own right. Until 1995 it ran on top of DOS, the actual OS, but no-one in their right mind would consider running DOS programs in Windows in this day and age. Console applications are not DOS programs, they are fully-fledged Windows programs but without a fancy GUI.
What is cin in c plus plus programming language?
We don't have to do anything fancy to implement this, we can simply extract the character sequence directly into a standard string object. By default, all leading whitespace characters (space, tab and newline) are ignored and extraction ends when the next whitespace or newline character is encountered in the character sequence.
#include<iostream>
#include<string>
int main () {
std::string s {};
std::cout << "Enter some text:\n";
std::cin >> s;
std::cout << "The first word input was:\n";
std::cout << s << std::endl;
}
Output:
Enter some text:
one two three
The first word input was:
one
What is the C plus plus program for regula falsi method?
#include
#include
#include
/* define prototype for USER-SUPPLIED function f(x) */
double ffunction(double x);
/* EXAMPLE for "ffunction" */
double ffunction(double x)
{
return (x * sin(x) - 1);
}
/* -------------------------------------------------------- */
/* Main program for algorithm 2.3 */
void main()
{
double Delta = 1E-6; /* Closeness for consecutive iterates */
double Epsilon = 1E-6; /* Tolerance for the size of f(C) */
int Max = 199; /* Maximum number of iterations */
int Satisfied = 0; /* Condition for loop termination */
double A, B; /* INPUT endpoints of the interval [A,B] */
double YA, YB; /* Function values at the interval-borders */
int K; /* Loop Counter */
double C, YC; /* new iterate and function value there */
double DX; /* change in iterate */
printf("-----------------------------------------------------\n");
printf("Please enter endpoints A and B of the interval [A,B]\n");
printf("EXAMPLE : A = 0 and B = 2. Type: 0 2 \n");
scanf("%lf %lf", &A, &B);
printf("The interval ranges from %lf to %lf\n", A,B);
YA = ffunction(A); /* compute function values */
YB = ffunction(B);
/* Check to see if YA and YB have same SIGN */
if( ( (YA >= 0) && (YB >=0) ) ( (YA < 0) && (YB < 0) ) ) {
printf("The values ffunction(A) and ffunction(B)\n");
printf("do not differ in sign.\n");
exit(0); /* exit program */
}
for(K = 1; K <= Max ; K++) {
if(Satisfied 0) { /* first 'if' */
Satisfied = 1; /* Exact root is found */
}
else if( ( (YB >= 0) && (YC >=0) ) ( (YB < 0) && (YC < 0) ) ) {
B = C; /* Squeeze from the right */
YB = YC;
}
else {
A = C; /* Squeeze from the left */
YA = YC;
}
if( (fabs(DX) < Delta) && (fabs(YC) < Epsilon) ) Satisfied = 1;
} /* end of 'for'-loop */
printf("----------------------------------------------\n");
printf("The number of performed iterations is : %d\n",K - 1);
printf("----------------------------------------------\n");
printf("The computed root of f(x) = 0 is : %lf \n",C);
printf("----------------------------------------------\n");
printf("Consecutive iterates differ by %lf\n", DX);
printf("----------------------------------------------\n");
printf("The value of the function f(C) is %lf\n",YC);
} /* End of main program */
How do you call multiple constructors with single object?
You can have any number of constructors for a class. All we need to do is implement constructor overloading.
Ex: let us say we want to create multiple constructor for a class Test
Public class Test {
Public Test() {
//code
}
Public Test(int vals) {
//code
}
Public Test(String val) {
//code
}
}