What is a Concrete class in c plus plus?
A concrete class is a complete type, as opposed to an abstract class which is an incomplete type.
class A {
public:
virtual void f () = 0; // pure-virtual; class A is abstract
// ...
};
class B : public A {
public:
void f () override; // overrides A::f(); class B is concrete
};
class C {
// no pure-virtual methods; C is a concrete type
// ...
};
Template classes are generic types. We usually evolve a generic type from a concrete type rather than write a template class from scratch, a technique known as lifting. The concrete type provides us with the complete implementation which is useful for testing and debugging purposes. When the concrete class is fully-implemented and error-free, we can generalise to produce the generic type.
How can the base class data be accessed from database directly using derived class?
In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.
Is run time initialization of an array an initialization or an assignment?
It is an initialisation. You cannot assign values to a dynamic array until it has been initialised. Static arrays can be initialised at compile time. They can also be assigned at compile time.
void main()
{
// Compile time:
// =========
// Initialise a static array.
int arr1[10];
// Initialise and assign a static array.
int arr2[10] = {0,1,2,3,4,5,6,7,8,9};
// Runtime:
// ======
// Dynamic array (size unknown, no memory allocated)
int* pArr[];
// Initialise (size known, allocate memory, 40 bytes):
pArr = ( int* ) malloc( 10 * sizeof( int ));
// Assign (set values of elements):
int x;
for(x=0;x<10;++x)
pArr[x] = x;
// Uninitialise (release memory).
free( pArr );
return( 0 );
}
What is an exception With an example program explain how it is handled?
An exception is literally that: an exception. Exceptions are thrown when something that shouldn't happen does. For example, if you're trying to turn "Hi" into an Integer, you'll get a NumberFormatException (in Java.) If there is nothing in place to handle an exception, the program will crash. Try-catch blocks are used for exception handling within methods, and exception throwing is used to give out exceptions, or let a different method handle it.
Example program (Java):
class ex{
public static void main(String[] args){
String Strx = "5a";
int x = 0;
try{
x += Integer.parseInt(Strx);
}catch(NumberFormatException e){ // If Strx is not an Integer
x += Integer.parseInt("5");
}
System.out.println("The value of x is " + x); // The value of x is 5
}
}
Alternatively, you could remove the try-catch blocks, and simply write "public static void main(String[] args) throws NumberFormatException" in the 2nd line of the program.
What is the sudoku solver c plus plus code?
The code is below and i should also explain the algorithm. Well, What we are doing here is that we already defined the size to be 9x9 sudoku and are getting values using loops. All the empty spots are given UNASSIGNED value. Then we have functions to tell that if it is safe to put a value in the empty box by calculation and according to the rules of Sudoku it checks for is there any other some number horizontally and vertically and do the sum of the row and column is less than or equal to required or not. If the functions returns true then the program puts the value there.
Do reference variables occupy memory in c?
All references must be non-null, therefore they will always have memory allocated to them. A reference is simply a non-null memory location that stores a specific type of variable, as determined by the reference's type. The reference name is an alias (a token) for the memory location, in much the same way that an array name is an alias for the starting address of the array.
A pointer variable is different in that memory must be set aside for the pointer variable itself, in addition to the memory it actually points to. On a 32-bit system, 4 bytes must be allocated to every pointer variable, to store the memory address that they point to, which could be null. But references only occupy the memory they actually refer to, which can never be null (a null reference will in fact render the program invalid).
Write a Program for Array implementation of queue and stack using exception handling.?
include <iostream>
using namespace std;
#define SIZE 10
template <class StackType> class stack {
StackType stck[SIZE];
int topOfStack;
public:
void init() {
topOfStack = 0;
}
void push(StackType ch);
StackType pop();
};
template <class StackType>
void stack<StackType>::push(StackType ob)
{
try {
if(topOfStack==SIZE) throw SIZE;
} catch(int) {
cout << "Stack is full.\n";
return;
}
stck[topOfStack] = ob;
topOfStack++;
}
template <class StackType>
StackType stack<StackType>::pop()
{
try {
if( topOfStack == 0)
throw 0;
} catch(int) {
cout << "Stack is empty.\n";
return 0;
}
topOfStack--;
return stck[topOfStack];
}
int main()
{
stack<char> stack1, stack2;
int i;
stack1.init();
stack2.init();
stack1.push('a');
stack2.push('x');
stack1.push('b');
stack2.push('y');
stack1.push('c');
stack2.push('z');
for(i = 0; i <3; i++)
cout << "Pop stack1: " << stack1.pop() << endl;
for(i = 0; i <4; i++)
cout << "Pop stack2: " << stack2.pop() << endl;
// demonstrate double stacks
stack<double> doubleValueStack1, doubleValueStack2; // create two stacks
// initialize the stacks
doubleValueStack1.init();
doubleValueStack2.init();
doubleValueStack1.push(1.1);
doubleValueStack2.push(2.2);
doubleValueStack1.push(3.3);
doubleValueStack2.push(4.4);
doubleValueStack1.push(5.5);
doubleValueStack2.push(6.6);
for(i = 0; i <3; i++)
cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl;
for(i = 0; i <4; i++)
cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl;
return 0;
}
Project - Settings - Link Tab Click on folder that has header files and librarys in the tree view Push OK
C plus plus program for displaying position and frequency of a number in the given list?
#include<iostream>
#include<vector>
#include<list>
#include<map>
#include<time.h>
int main()
{
srand ((unsigned)time(nullptr));
// Create a list of 100 values in the range 0 to 9.
std::cout << "Number list:\t";
std::list<size_t> numbers;
for (size_t i=0; i<100; ++i)
{
numbers.push_back (rand()%10);
std::cout << numbers.back() << ' ';
}
std::cout << '\n' << std::endl;
// Determine position(s) of each number.
std::map<size_t, std::vector<size_t>> map;
size_t pos = 0;
for (auto it=numbers.begin(); it!=numbers.end(); ++it)
map[*it].push_back(pos++);
// Print number, frequency and position(s).
for (auto it=map.begin(); it!=map.end(); ++it)
{
std::cout << "Number:\t\t" << (*it).first << std::endl;
std::cout << "Frequency:\t" << (*it).second.size() << std::endl;
std::cout << "Postions:\t";
const std::vector<size_t>& positions = (*it).second;
for (auto it=positions.begin(); it!=positions.end(); ++it)
std::cout << (*it) << ' ';
std::cout << '\n' << std::endl;
}
}
Can you assigned value of int to a ordinary pointer?
Yes, with type-cast (but I don't see why you should):
char *ptr = (char *)300;
What must identifiers start with?
the name of an identifier consists of letters and digits but name always starts with a letter.
How many types of selection statements in c language?
there are two types of selection statements in c-language they are if-else and switch case.these two are known as selection statements,because their syntax is like this,
if-else syntax:-if(condition)
{
statements
}
else
{
statement
}
in the abow syntax it contains two blocks first one is if and second one is else in 'if' it fills with required condition if the condition is satisfied then it execute the if block,whether it is false it execute the else statements.
switch case syntax:-
switch(condition)
{
case.1:statements;
case.2:statements;
case.3:statements;
.
.
.
case.n:statements;
default:statement;
}
in abow switch case we can select any case and execute,in this selection statement gives the fiexibulity to the user.it contains default statement it shows the wrong selection in these cases.
examples programs to these statements:-
1.biggest of two numbers program?
main()
{
int a,b,c;
printf("enter a,b values");
scanf(&a);
scanf(&b);
scanf(&c);
if(a>b)
{
printf("a is big");
}
else
{
printf("b is big");
}
}
out put:-
enter a,b values 8,5
a is big
enter a,b values 3,9
b is big
2.perform arithmetic operation intwo numbers?
main()
{
int a,b,c,d;
printf("enter a,b values");
scanf(&a);
scanf(&b);
scanf(&c);
printf("enter your choice"&c);
switch(c)
{
case.1:d=a+b;
printf("d value is"&d);
case.2:d=a-b;
printf("d value is"&d);
case.3:d=a*b;
printf("d value is"&d);
default:printf("wrong selection");
}
}
out put:-
enter a,b values 6,5
enter your choice 1
d=11
enter your choice 2
d=1
enter your choice 3
d=30
A compound statement that is formed by joining two statements with the word and is called a?
conjunction...your welcome
Which header file needs to be included in a program that uses the data types ifsteam and ofsteam?
#include <fstream>
How do we write c program without using swap to interchange values by accessing another variable?
int a,b;
a=a+b;
b=a-b;
a=a-b;
that's it simple
What does the delete operator do in addition to deallocation of memory space?
The delete operator calls the destructor of the object referenced by its operand. If the destructor is virtual, the destructor of each superclass up to the top of the inheritance hierarchy is also called, in order. If you don't define a destructor for a class, the compiler defines a default destructor that has no effect. Fundamental types (char, int, float, etc.) do not have destructors, so using delete has no other effects.
As an aside: when you use inheritance, make sure to make your destructors virtual, so that objects are properly destroyed!
Also note that you should not use C's free() on a pointer that you got from C++'s new, or use C++'s delete on a pointer you got from C's malloc(). These are not guaranteed to work, and mixing them might cause Big Bad Things to happen. In general, there is no reason to use malloc()/free() in C++ at all.
No. The subtraction operator is a binary operator that returns the result of subtracting the rhs operand from the lhs operand. The unary minus operator simply negates the rhs operand.
int x = -5; // unary minus. x is (-5)
int y = -x; // unary minus. y is (+5)
y -= x; // binary minus/assign operator. y is (+10)
--x; // unary decrement operator. x is (-6)
y -= (-x); // binary minus/assign and unary minus operators. y is(+4)
What is the advantage of cin getline over cin?
The getline() function reads a string of characters up to a newline character or a predefined delimiter. Although extracting directly from cin is faster, the onus is on the programmer to ensure the input is valid at all times, and to clear the buffer before accepting new input. Keyboard entry is particularly problematic, thus it is easier to treat each input as a string which can be easily verified and convert to a numeric value where required.
stray pointer is a that pointer which pin points nothing or anywhere but we dont know...
for example:
int *ptr;
ptr=new int[10]; //this memory is on heap.
and at the end of the programm if we dont delete this memory mean to say
if we dont deallocate this memory then this type of pointer is pointing towards nothing or anywhere because when we work on heap deletion of pointer is must if we dont delete pointers than they pin point stray or anywhere so that sort of pointer is stray pointer.
i think you understand...
How do we tuple initialize a class or structure?
Consider the following structure:
struct X {
int a;
double b;
// ...
};
Here we could initialise with a std::tuple<int, double>. To achieve this we simply define a constructor that accepts the required tuple:
#include<tuple>
struct X {
int a;
double b;
X::X (std::tuple<int, double>& t): a {std::get<0>(t)}, b {std::get<1>(t)} {}
// ...
};
Note that any constructor that has one argument is known as a conversion constructor, in this case converting from tuple to X. It is usually a good idea to declare such constructors explicit, particularly if you also provide the complementary conversion operator (from X to tuple).
#include<tuple>
struct X {
int a;
double b;
explicit X::X (const std::tuple<int, double>& t): a {std::get<0>(t)}, b {std::get<1>(t)} {} operator std::tuple<int, double> (void) const {return std::make_tuple (a, b);}
// ...
};