In this blog I am providing you major concepts of of Java, C++, C & Algorithms etc.
Sunday, 23 January 2022
Java Database Connectivity Example
NET December 2021 Computer Science Solved Paper 2 Question 1
Q: 1 Let us assume a person climbing the stairs can take one stair or two stairs at a time. How many ways can this person climb a flight of eight stairs?
(a) 21
(b) 24
(c) 31
(d) 34
Answer : d
Solution:
First Method :
If number of stairs = n then total ways will be fib(n+1).
Here, we have to use the concept of combination to find the number of ways in which a person can walk up a stairway which has 8 steps. So, we have to make
pairs of the combination of 1 and 2 steps and then by using the concept of the combination we will get the number of ways a person can walk up a stairway of 8 steps.
It is given that there are 8 steps of the stairway and he can take only 1 or 2 steps up the
stairs at a time.
Friday, 21 January 2022
UGC-NET Computer Science December 2014 Paper 2 with Solution
3 or by 5 or by both 3 and 5 ?
(A) 533
(B) 599
(C) 467
(D) 66
Answer: (C)
Explanation:
Given Set is A = {1, 2, 3, …….., 1000}.
The members of 'A' which are divisible by 3 are given by {3,6,9,12...,999} these are total 333 elements.
The members of 'A' which are divisible by 5 are given by {5,10,15,20,...,1000} these are total 200 elements.
The members of 'A' which are divisible by both 3 and 5 i.e. divisible by 15 are given by {15,30,45,...,990} these are total 66 elements.
So, we have to find How many members of A shall be divisible by
3 or by 5 or by both 3 and 5?
i.e. No. of elements divisible by 3 + No. of elements divisible by 5 - No. of elements divisible by 15.
(Union operation n(X union Y)=n(X)+n(Y) - n(X intersection Y))
=> 333 + 200 - 66=533 - 66 = 467.
Hence correct option is (C).
Q : 2 A certain tree has two vertices of degree 4, one vertex of degree 3 and one vertex of
degree 2. If the other vertices have degree 1, how many vertices are there in the graph ?
(A) 5
(B) n – 3
(C) 20
(D) 11
Answer: (D)
Explanation:
We know that in a tree the total number of edges are always 1 less than total number of vertices.
Also according to handshaking theorem:
Sum of degrees of all the vertices in a graph is equal to twice the number of edges.
So, by applying these two concepts together let total number vertices be 'n'.
=> 2 * 4 + 1 * 3 + 1 * 2 + 1 * ( n - 4 ) = 2*( n - 1 )
=> 8 + 3 + 2 + n - 4 = 2n - 2
=> 13 - 4 + n = 2n - 2
=> n = 11.
Hence correct option is (D).
Q : 3 Consider the Graph shown below :
This graph is a __________.
(A) Complete Graph
(B) Bipartite Graph
(C) Hamiltonian Graph
(D) All of the above
Answer: (C)
Explanation:
Let us examine all the options one by one.
(A) Complete Graph: It is a graph in which each vertex is connected to all the other vertices in the graph. But in the given graph there is no direct edge between (A,C), (B,D) So, it is not a complete graph. This option is wrong also option (D) becomes incorrect.
(B) Bipartite Graph: It is a graph whose vertices can be divided into two disjoint sets and (that is, and are each independent sets) such that every edge connects a vertex in to one in . Vertex set and are often denoted as partite sets. This option is also incorrect.
(C) Hamiltonian Graph: A graph is a Hamiltonian graph if it contains a Hamiltonian circuit or cycle.
Hamilton circuit, is a graph cycle (i.e., closed loop) through a graph that visits each node exactly once. The above graph has Hamiltonian circuit A->F->B->C->E->D->A. Hence it is a Hamiltonian Graph. So, option (C) is correct.
(B) x8 – 73 and x8 – 73
(D) x8 + 73 and x8 + 73
Answer: (D)
Explanation:
We have to compute ho(gof), firstly we will calculate gof which is g(x4) = √x 8 + 1.
Now we will calculate ho(gof) i.e. h(√x 8 + 1) = (√x 8 + 1)2 + 72= x 8 + 73.
So, option (D) is correct.
Q : 11 What will be the output of the following ‘C’ code ?
main ( )
{
int x = 128;
printf ("\n%d", 1 + x ++);
}
(A) 128
(B) 129
(C) 130
(D) 131
Answer: (B)
Explanation:
Firstly variable 'x' is assigned 128, then in printf("\n%d",1+x++) here '++' is used as post increment operator this means when the expression '1+x++' is evaluated 'x++' is substituted as 128 and '1+128' becomes 129. After the printf is executed 'x' will become 129 as a result of post increment '++' operator. Hence correct option is (B).
Q : 12 What does the following expression means ?
char *(*(* a[N]) ( )) ( );
(A) a pointer to a function returning array of n pointers to function returning character pointers.
(B) a function return array of N pointers to functions returning pointers to characters
(C) an array of n pointers to function returning pointers to characters
(D) an array of n pointers to function returning pointers to functions returning pointers to characters.
Answer: (D)
Explanation:
By clearly examining the above question let us begin from the inner most parenthesis which indicates that it is an array of 'N' pointers to the function, moving one step out it shows it is again returning pointer to function and in the end it indicates the return type of function which is char pointer. So, we can say that it represents an array of n pointers to function returning pointers to functions returning pointers to characters. Hence correct option is (D).
Q : 13 Which of the following is not a member of class ?
(A) Static function
(B) Friend function
(C) Const function
(D) Virtual function
Answer: (B)
Explanation:
Let us examine all the options one by one. First option is Static function it is a function which does not belong to an object but it is shared by all the objects and can be accessed by class-name::function-name. No need to create object of the class it is a member of class. This option is incorrect.
Next option is Friend function it is a function which is defined outside the class and it is not a member of the class. It is used to access data of the other class whose friend it is declared.
So, it is the correct answer.
Next is Const function this function cannot change or modify anything it remains constant and can only be used for printing output purpose. It is also a member of class so this option is also incorrect.
Next is Virtual function this is a type of function which is generally declared to resolve the ambiguity in case of hybrid inheritance so that only a single copy of the function is inherited.
It is also a member of class hence this option is also incorrect.
We can say that friend function is not a member of class but it require an object of the class to access its data to whom it is declared as friend.
Q : 14 When an array is passed as parameter to a function, which of the following statements is correct ?
(A) The function can change values in the original array.
(B) In C, parameters are passed by value, the function cannot change the original value in the array.
(C) It results in compilation error when the function tries to access the elements in the array.
(D) Results in a run time error when the function tries to access the elements in the array.
Answer: (B)
Explanation:
Let us examining all the options.
First option is correct as by default in 'C' arrays are passed by address so it can change the values in the original array. Normal variables are passed by value in 'C'.
void change(int a[],int n);
void change(int *a,int n);
Second option is incorrect in case of arrays as we explained above.
Similarly C,D both are incorrect.
Q : 15 Which of the following differentiates between overloaded functions and overridden
functions ?
(A) Overloading is a dynamic or runtime binding and overridden is a static or compile time binding.
(B) Overloading is a static or compile time binding and overriding is dynamic or runtime binding.
(C) Redefining a function in a friend class is called overloading, while redefining a function in a derived class is called as overridden function.
(D) Redefining a function in a derived class is called function overloading, while redefining a function in a friend class is called function overriding.
Answer: (B)
Explanation:
Function overloading means having more than one same name functions but with different parameters. Return type alone is not sufficient for function overloading. It is done at compile time.
Function overriding is defined as when derived class has same name method as in base class with same signature then definition of function in derived class overrides the definition in base class.
It is done at run-time. Hence correct option is (B).
Q : 21 Convert the following infix expression into its equivalent post fix expression
(A + B^ D) / (E – F) + G ?
(A) ABD^ + EF – / G+
(B) ABD + ^EF – / G+
(C) ABD + ^EF / – G+
(D) ABD^ + EF / – G+
Answer: (A)
Explanation:
To convert this expression into post fix expression firstly we should consider the priority of all the operators in the expression. The operator with highest priority will be considered first.
The expression part which is converted into its post fix notation has been underlined.
Highest priority is of brackets, let us consider (A + B^ D) in this bracket further the highest priority is of '^' so it will converted first the whole expression would look like:
(A + BD^) / (E – F) + G
=> ABD^+ / EF- + G
=>ABD^+EF-/ + G
=>ABD^+EF-/ G+
Hence Final result is ABD ^ + EF - / G +, So correct option is (A).
Q : 24 Consider an array A[20, 10], assume 4 words per memory cell and the base address of
array A is 100. What is the address of A[11, 5] ? Assume row major storage.
(A) 560
(B) 565
(C) 570
(D) 575
Answer: (A)
Explanation:
In computers memory the array elements are always stored linearly whether they are 1-Dimensional or 2-Dimensional. Like this is an example of 2-Dimensional array stored in memory linearly:
As we can see element A11 is stored at location '0' in memory and it is known as its offset.
Step 1:
Now to calculate the offset in case of Row major order we will use the formula:
Offset = (Row No. * Total No. of Columns) + Column No.
Now to calculate the offset in case of Column major order we will use the formula:
Offset = (Column No. * Total No. of Rows) + Row No.
'Row No.' is the row number of the location whose address we need to calculate.
'Column No.' is the column number of the location whose address we need to calculate.
'Total No. of Columns' is the total number of columns in the given matrix.
'Total No. of Rows' is the total number of rows in the given matrix.
Step 2:
Multiply the offset calculated with the size of each memory word.
Step 3:
Add the Base address to calculate the effective address.
Given matrix is A[20][10], Size of memory word is 4 bytes, address of the location to find A[11][5], Base address is 100.
Step 1:
Offset = (11 * 10) + 5 = 115.
Step 2:
115 * 4 = 460.
Step 3:
100 + 460 = 560.
So, correct option is (A) 560.
Q : 25 A full binary tree with n leaves contains
(A) n nodes
(B) log2n nodes
(C) 2n –1 nodes
(D) 2n nodes
Answer : (C)
Explanation :
A full binary tree is one in which each node has exactly two children. Like on given below is a full binary tree with 15 nodes:
No. of internal nodes are given by 'No. of External or Leaf nodes - 1'.
In our question we have to find total no. of nodes in the tree. We are given that total no. of leaf nodes are 'n'. Total no. of nodes in tree = Total No. of Leaf Nodes + Total No. of Internal Nodes
=> Total no. of nodes in tree = n + n - 1 = 2n-1.
We can examine this by putting the no. leaf nodes of the above tree in given formula which is 8.
Total no. nodes in tree = 2 * 8 - 1 = 16 - 1 = 15.
Hence correct option is (C).
Q : 26 The period of a signal is 10 ms. What is its frequency in Hertz ?
(A) 10
(B) 100
(C) 1000
(D) 10000
Answer: (B)
Explanation:
We know that period and frequency are reciprocals of each other.
f = 1/T
1 sec = 1 hertz
1 millisecond = 1000 Hz = 1 KHz
1 microsecond = 1000000 hertz = 1 MHz
In our question Period is 10 ms.
10 ms = 10 * 10-3 sec = 10-2 sec
f = 1/10-2 = 100 Hz.
Hence correct option is (B).
Q : 28 Which of the following algorithms is not a broadcast routing algorithm ?
(A) Flooding
(B) Multidestination routing
(C) Reverse path forwarding
(D) All of the above
Answer:
Explanation:(B)
Flooding:
Flooding is a simple computer network routing algorithm in which every incoming packet is sent through every outgoing link except the one it arrived on. It is a broadcast routing algorithm, So, this option is incorrect.
Multi destination routing:
Algorithms for effectively routing messages from a source to multiple destination nodes in a store-and-forward computer network are studied. The focus is on minimizing the network cost (NC), which is the sum of weights of the links in the routing path. Several heuristic algorithms are studied for finding the NC minimum path (which is an NP-complete problem). It is not a broadcasting routing algorithm So, it is the correct option.
(A) 0.336 bits/baud
(C) 120,00,000 bits/baud
(A) 16
(C) 18
(A) Generated in first pass
(B) Generated in second pass
(C) Not generated at all
(D) Generated and used only in second pass
Consider an assembler instruction like the following
JMP LATER ... ... LATER:
This is known as a two-pass assembler. Each pass scans the program, the first pass generates the symbol table and the second pass generates the machine code.
Hence correct option is (A).
Q : 34 Debugger is a program that
(A) allows to examine and modify the contents of registers
(B) does not allow execution of a segment of program
(C) allows to set breakpoints, execute a segment of program and display contents of register
(D) All of the above
Answer: (C)
Explanation:
Debugger: A special program used to find errors (bugs) in other programs. A debugger allows a programmer to stop a program at any point and examine and change the values of variables.
Typically, debuggers also offer more sophisticated functions such as running a program step by step (single-stepping or program animation), stopping (breaking) (pausing the program to examine the current state) at some event or specified instruction by means of a breakpoint, and tracking the values of some variables. Some debuggers have the ability to modify the state of the program while it is running, rather than merely to observe it. It may also be possible to continue execution at a different location in the program to bypass a crash or logical error.
Hence correct option is (C).
Q : 35 The following Context-Free Grammar (CFG) :
S -> aB | bA
A -> a | aS | bAA
B -> b | bS | aBB
will generate
(A) odd numbers of a’s and odd numbers of b’s
(B) even numbers of a’s and even numbers of b’s
(C) equal numbers of a’s and b’s
(D) different numbers of a’s and b’s
Answer: (C)
Explanation:
S -> aB -> ab
S -> aB -> abS -> abaB -> abab
S -> aB -> abS -> abaB -> abaaBB -> abaabb
S -> bA -> ba
S -> bA -> baS -> babA -> baba
Like this we can prove it for each rule and it will always generate equal numbers of a’s and b’s.
Hence correct option is (C).
Q : 38 Which of the following conditions does not hold good for a solution to a critical section
problem ?
(A) No assumptions may be made about speeds or the number of CPUs.
(B) No two processes may be simultaneously inside their critical sections.
(C) Processes running outside its critical section may block other processes.
(D) Processes do not wait forever to enter its critical section.
Answer: (C)
Explanation:
The Critical-Section Problem:
Consider a system consisting of n processes {P0, P1, ..., Pn−1}. Each process has a segment of code, called a critical section, in which the process may be changing common variables, updating a table, writing a file, and so on. The important feature of the system is that, when one process is executing in
its critical section, no other process is to be allowed to execute in its critical section. That is, no two processes are executing in their critical sections at the same time. The critical-section problem is to design a protocol that the processes can use to cooperate. Each process must request permission to enter its critical section. The section of code implementing this request is the entry section. The critical section may be followed by an exit section. The remaining code is the remainder section. The general structure of a typical process Pi is shown here:
A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual exclusion: If process Pi is executing in its critical section, then no other processes can be executing in their critical sections.
We assume that each process is executing at a nonzero speed. However,we can make no assumption concerning the relative speed of the 'n' processes.
Answer: (C)
Explanation:
- Small size: less wasted space due to internal fragmentation; fewer unused pages in memory.
- Large page size: more efficient disk I/O, smaller page tables
xz/y + y/2To minimize the overhead, differentiate with respect to page size, y, and equate to 0:
-xz/y2 + 1/2 = 0So for example, if the average segment size were 256 K, and the page table entry size were 8 bytes, the optimum page size, to minimize overhead due to page table entries and internal fragmentation, would be √(2 × 256K × 8) = 2048 = 2K.
=> y = √(2xz)
(A) 11.6 m.sec.
(B) 16.4 m.sec.
(C) 28 m.sec.
(D) 14 m.sec.
Answer: (B)
Explanation:
Time to serve page fault if frame is empty or page replaced is not modified = 8 m.sec
Time to serve page fault if page is modified = 20 m.sec
Frequency of Page modification = 70% = 70/100 = 0.7
Frequency when page is not modified or frame is empty = (100-70)% = 30% = 30/100 = 0.3
Average access time = (0.3 * 8) + (0.7 * 20) = 2.4 + 14 = 16.4 m.sec
Hence correct option is (B) 16.4 m.sec.
Q : 41 __________ are applied throughout the software process.
(A) Framework activities
(B) Umbrella activities
(C) Planning activities
(D) Construction activities
Answer: (B)
Explanation:
Any standard software process model would primarily consist of two types of activities: A set of framework activities, which are always applicable, regardless of the project type, and a set of umbrella activities, which are the non SDLC activities that span across the entire software development life cycle.
The umbrella activities in a software development life cycle process include the following:
- Software Project Management.
- Formal Technical Reviews.
- Software Quality Assurance.
- Software Configuration Management.
- Re-usability Management.
- Risk Management.
- Measurement and Metrics.
- Document Preparation and Production
CMM Level | Focus | Key Process Ares |
---|---|---|
1. Initial
|
Competent people
| |
2. Repeatable
|
Project management
|
Software project planning
Software Configuration
Management
|
3. Defined
|
Definition of Processes
|
Process definition
Training program Peer reviews
|
4. Managed
|
Product and Process Quality
|
Quantitative Process Metrics
Software Quality Management
|
5. Optimizing
|
Continuous Process Improvement
|
Defect Prevention
Process Change Management
Technology change management
|
Hence correct option is (C).
Q : 43 The software _________ of a program or a computing system is the structure or structures of the system, which comprise software components, the externally visible properties of those components, and the relationships among them.
(A) Design
(B) Architecture
(C) Process
(D) Requirement
Answer:
Explanation:
Software Architecture:
Software architecture refers to the high level structures of a software system, the discipline of creating such structures, and the documentation of these structures. These structures are needed to reason about the software system. Each structure comprises software elements, relations among them, and properties of both elements and relations. The architecture of a software system is a metaphor, analogous to the architecture of a building.
Software architecture is about making fundamental structural choices which are costly to change once implemented. Software architecture choices include specific structural options from possibilities in the design of software. For example, the systems that controlled the space shuttle launch vehicle had the requirement of being very fast and very reliable. Therefore, an appropriate real-time computing language would need to be chosen. Additionally, to satisfy the need for reliability the choice could be made to have multiple redundant and independently produced copies of the program, and to run these copies on independent hardware while cross-checking results.
Documenting software architecture facilitates communication between stakeholders, captures early decisions about the high-level design, and allows reuse of design components between projects.
Hence correct option is (B).
Q : 44 Which one of the following set of attributes should not be encompassed by effective software metrics ?
(A) Simple and computable
(B) Consistent and objective
(C) Consistent in the use of units and dimensions
(D) Programming language dependent
Answer: (D)
Explanation:
Software Metrics should never be Programming Language Dependent as in each programming language syntax, functions, operators, statements may differ a lot.
Hence correct option is (D).
Q : 45 Which one of the following is used to compute cyclomatic complexity ?
(A) The number of regions – 1
(B) E – N + 1, where E is the number of flow graph edges and N is the number of flow graph nodes.
(C) P – 1, where P is the number of predicate nodes in the flow graph G.
(D) P + 1, where P is the number of predicate nodes in the flow graph G.
Answer: (D)
Explanation:
Looking at first option which is incorrect it should be The number of regions + 1.
In second option which is also incorrect it should be E – N + 2P, where E is the number of flow graph edges and N is the number of flow graph node.
S1 : A hard handover is one in which the channel in the source cell is retained and used
for a while in parallel with the channel in the target cell.
S2 : A soft handover is one in which the channel in the source cell is released and only
then the channel in the target cell is engaged.
(A) S1 is true and S2 is not true.
(C) Both S1 and S2 are true.
(C) keys and measures
Explanation:
A factless fact table is a fact table that does not have any measures. It is essentially an intersection of dimensions. On the surface, a factless fact table does not make sense, since a fact table is, after all, about facts. However, there are situations where having this kind of relationship makes sense in data warehousing.
For example, think about a record of student attendance in classes. In this case, the fact table would consist of 3 dimensions: the student dimension, the time dimension, and the class dimension. This factless fact table would look like the following:
The only measure that you can possibly attach to each combination is "1" to show the presence of that particular combination. However, adding a fact that always shows 1 is redundant because we can simply use the COUNT function in SQL to answer the same questions.
Factless fact tables offer the most flexibility in data warehouse design. For example, one can easily answer the following questions with this factless fact table:
How many students attended a particular class on a particular day?
How many classes on average does a student attend on a given day?
Without using a factless fact table, we will need two separate fact tables to answer the above two questions. With the above factless fact table, it becomes the only fact table that's needed.
services ?
(A) B2 B
(C) C2 C
A few examples of B2B product based companies would be:
Customer to Customer (C2C) markets are innovative ways to allow customers to interact with each other. While traditional markets require business to customer relationships, in which a customer goes to the business in order to purchase a product or service. In customer to customer markets the business facilitates an environment where customers can sell these goods or services to each other.
Consumer to Consumer (or citizen-to-citizen) electronic commerce involves the electronically facilitated transactions between consumers through some third party. A common example is the online auction, in which a consumer posts an item for sale and other consumers bid to purchase it; the third party generally charges a flat fee or commission. The sites are only intermediaries, just there to match consumers. They do not have to check quality of the products being offered.
Consumer to Consumer (C2C) marketing is the creation of a product or service with the specific promotional strategy being for consumers to share that product or service with others as brand advocates based on the value of the product. The investment into concepting and developing a top of the line product or service that consumers are actively looking for is equitable to a retail pre-launch product awareness marketing.
C2B model, also called a reverse auction or demand collection model, enables buyers to name or demand their own price, which is often binding, for a specific good or service. The website collects the demand bids then offers the bids to participating sellers.
customers who buy a particular product at once.
(A) Economic Order Quantity
(C) Data Mining
'Demand-Sensitive Pricing' or 'Price Elasticity Of Demand'
A measure of the relationship between a change in the quantity demanded of a particular good and a change in its price. Price elasticity of demand is a term in economics often used when discussing price sensitivity. The formula for calculating price elasticity of demand is:
Price Elasticity of Demand = % Change in Quantity Demanded / % Change in Price
If a small change in price is accompanied by a large change in quantity demanded, the product is said to be elastic (or responsive to price changes). Conversely, a product is inelastic if a large change in price is accompanied by a small amount of change in quantity demanded.
Hence correct option is (D).
Q : 50 Match the following :
List – I List – II
a. Call control protocol i. Interface between Base Transceiver Station
(BTS) and Base Station Controller (BSC)
b. A-bis ii. Spread spectrum
c. BSMAP iii. Connection management
d. CDMA iv. Works between Mobile Switching Centre
(MSC) and Base Station Subsystem (BSS)
Codes :
a b c d
(A) iii iv i ii
(B) iii i iv ii
(C) i ii iii iv
(D) iv iii ii i
Answer: (B)
Thursday, 20 January 2022
Copy Constructor in C++
When we want to initialize an object with use of some other object then copy constructor may be used.
It require reference to the object who's values will be copied to create new object. However it can also work without reference variable.
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
abc()
{
a=0;
b=0;
}
abc(int x,int y)
{
a=x;
b=y;
}
abc(abc &k)//copy constructor
{
a=k.a;
b=k.b;
}
void show()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
};
void main()
{
clrscr();
int l,m;
cout<<"Enter two no." <<endl;
cin>>l>>m;
abc ob1,ob2(l,m),ob3(ob2);
ob1.show();
ob2.show();
ob3.show();
getch();
}
In which case Copy Constructor is executed
1. When we pass object to a constructor then copy constructor provided by the programmer is executed.
2. When object is passed to a function
3. When object is assigned to another object using = operator
Dynamic Constructor in C++
Whenever value passed to a parameterized constructor is provided at runtime it is known as dynamic constructor
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
abc()
{
a=0;
b=0;
}
abc(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
};
void main()
{
clrscr();
int l,m;
cout<<"Enter two no." <<endl;
cin>>l>>m;
abc ob1,ob2(l,m);//dynamic constructor
ob1.show();
ob2.show();
getch();
}
In main function l & m will be initialized at runtime.
Parameterized Constructor in C++
Whenever we want to initialize data members with some values we use parameterized constructors.
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
abc()//default
{
a=0;
b=0;
}
abc(int x,int y)//parameterized
{
a=x;
b=y;
}
void show()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
};
void main()
{
clrscr();
abc ob1,ob2(10,20);
ob1.show();//it will print 0 0
ob2.show();//it will print 10 20
getch();
}
a & b of ob1 will be initialized to 0
a & b of ob2 will be initialized to 10 & 20 respectively.
Note: As we are creating a parameterized constructor we always need to provide default constructor.
Types of Constructors in C++
//Default Constructor By Programmer
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
abc()
{
a=10;
b=20;
}
void show()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
};
void main()
{
clrscr();
abc ob;
ob.show();
getch();
}
The above example shows that a & b will always be initialized to 10 & 20. It is default constructor by the programmer.
If we do not provide any constructor then default constructor by the compiler will initialize a & b with garbage value.
Constructors & Destructors in C++
Constructors are special member functions which are executed automatically when object of a class is created. These are special because they have same name as name of class. These are also special because they do not have any return type.
Syntax:
class-name(parameter list)
{
...
}
Eg:
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
abc()
{
a=0;
b=0;
}
void show()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
};
void main()
{
clrscr();
abc ob;
ob.show();
getch();
}
Types of Constructors in C++: