Impact of type modifiers on base data types.

Q4. What is the impact of type modifiers on base data types? Support your answer with examples for each data type.

Ans. We use a modifier to alter the meaning of the base type to fit various situations more precisely.

The list of modifiers is given below:

signed

unsigned

long

short

 

  1. Integer Type Modifiers

By using different number of bytes to store values, C++ offers three types of integers: short, int and long that can represent up to three different sizes. Each comes in both signed and unsigned versions. That gives you a choice of six different integer types.

Read More …

Difference between a variable, reference variable, a constant variable and a pointer variable.

Q3. Explain the difference between a variable, reference variable, a constant variable and a pointer variable. Support your answer with examples.

Ans.

Variable

Variables represent named storage locations, whose values can be manipulated during program run. For instance, to store name of a student and marks of a student during a program run, we require storage locations that too named so that these can be distinguished easily. Variables, called as symbolic variables, serve the purpose. The variables are called symbolic variables because these are named locations. For instance, the following statement declares a variable i of the data type int:

int i;

 

Reference Variable

A reference is an alternative name for an object. A reference variable provides an alias for a previously defined variable. A reference declaration consists of a base type, an & (ampersand), a reference variable name equated to a variable name. The general form of declaring a reference variable is Read More …

User-defined data types in C++ with examples.

Q2. Explain user-defined data types in C++ with appropriate examples.

Ans. There are some derived data types that are defined by the user. These are: Class, Structure, Union and Enumeration.

  1. Class

A class represents a group of similar objects. To represent classes in C++, it offers a user-defined data type called class. Once a class has been defined in C++, objects belonging to that class can easily be created. A class bears the same relationship to an object that a type does to a variable.

To define a class you describe what sort of information it can represent and what sort of actions you can perform with that data. For instance, if you want to create a class department. It can represent information like name of the department, number of its employee, name of its head and actions that can be performed with this information are : addition, deletion, modification of employee data, printing of department report etc.. now this class will be declared in C++ as follows:

 

class department

{

char name[20];

int num_emp;

char h_o_d[20];

public:

add();

delete(); Read More …

C++ data types with examples.

Q1. Explain C++ data types with appropriate examples.

Ans. C++ like any other language provides ways and facilities to handle different types of data by providing data types.

Data types are means to identify the type of data and associated operations of handling it.

C++ data types are of two types:

  • Fundamental types
  • Derived types

 

  1. Fundamental Data Types

Fundamental (atomic) data types are those that are not composed of other data types.

Five fundamental data types are:

  • int Data Type (for integers)

Integers are whole numbers such as 5, 39, -1917, 0 etc. they have no fractional parts. Integers in C++ by int data type. An identifier declared as int cannot have fractional part. Integers can be positive or negative.

  • char Data Type (for characters)

Characters can store any member of the C++ implementation’s basic set. If a character from this set is stored in a character variable, its value is equivalent to the integer code of that character. An identifier declared as char becomes a character variable.

  • float Data Type (for floating-point numbers)

A number having fractional part is a floating-point number. For example, 3.14159 is a floating-point number. The decimal point signals that it is a floating-point number not an integer. Floating-point numbers can also be written in exponent notation. For example, 147.9101 would be written as 1.479101E02. an identifier declared as float becomes a floating-point variable and can hold floating-pint numbers. Floating-point variables represent real numbers, which are used for measurable quantities like distance, area, temperature etc. and typically have a fractional part.

  • double Data Type (for double precision floating-point numbers)

The data type double is also used for handling floating-point numbers. But it is treated as a distinct data type because, it (double data type) occupies twice as much memory as type float, and stores floating-point numbers with much larger range and precision (significant numbers after decimal point). It stands for double precision floating-point. It is used when type float is too small or insufficiently precise.

  • void Data Type (for empty set of values and non-returning functions)

The void type specifies an empty set of values. It is used as the return type for functions that do not return a value. No object of type void may be declared.

 

 

  1. Derived Data Type

From the fundamental types other types can be derived by using the declaration operators.

  • Array

Arrays refer to a named list of a finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0, 1, 2, 3,….,n. If the name of an array of 10 elements is ARY, then its elements will be referenced as shown below:

ARY[0], ARY[1], ARY[2], ARY[3],…….,ARY[9]

Array can be one dimensional, two dimensional or multi dimensional.

For a type A, T[size] is the array of size elements of type A. the elements are indexed from 0 to size-1.

For example,

float a[3];                  /*declares array of three floats : a[0], a[1], a[2] */

 

int b[2][4];                 /*declares a two dimensional array of integrs:

b[0][0], b[0][1], b[0][2], b[0][3]

b[1][0], b[1][1], b[1][2], b[1][3]*/

 

Here, there are two integer arrays b[0] and b[1] each having 4 integer elements.

 

Char n[10];   /* declares a character array of size 10 */

  • Functions

A function is a named part of a program that can be invoked from other parts of the program as often needed.

  • Pointers

A pointer is a variable that holds a memory address. This address is usually the location of another variable in memory. If one variable contains the address of another variable, the first variable is said to pint to the second.

 

 

If a variable is going to hold a pointer, it must b declared as such. A pointer declaration consists of a base type, an * (asterisk), and the variable name. The general form of declaring a pointer variable is

type *ptr;

where type is any valid C++ type and ptr is the name of the pointer variables.

For example:

char *a;

declares a pointer to character.

 

The base type of the pointer defines what type of variables the pointer can point to. Technically, any type of pointer can point anywhere in memory. However, all pointer arithmetic is done relative to its base type so it is important to declare the pointer correctly.

 

  • References

A reference is an alternative name for an object. A reference variable provides an alias for a previously defined variable. A reference declaration consists of a base type, an & (ampersand), a reference variable name equated to a variable name. The general form of declaring a reference variable is

type & ref-var = var-name;

where type is any valid C++ data type, ref-var is the name of reference variable that will point to variable denoted by var-name.

For example, if sum is declared as a reference variable for a variable total, then both sum and total represent the same variable and can be used interchangeably. Following program segment illustrates it:

int total;

int &sum = total;

total = 100;

cout,,”Sum = “<<sum<<”\n”;

cout<<”Total = “<<total<<”\n”;

 

The output of above program segment will be as follows:

Sum = 100

Total = 100

Both the variables refer to the same data object in the memory, thus, print the same value.

  • Constants

The keyword const can be added to the declaration of an object to make that object a constant rather than a variable. Thus, the value of the named constant cannot be altered during the program run. The general form of constant declaration is as follows:

const type name = value;

where const is the keyword that must be used for declaring a constant, type is any valid C++ data type, name is the name of the constant and value is the constant value of the data type type. For instance,

const int upperage = 50;

declares a constant named a upperage of type integer that holds value 50;

a constant must be initialized at the time of declaration. If you give only const in place of const int, it means the same.

C++ program to print ASCII code for given digit and backspace.

Q28. Write a program to

  • print ASCII code for a given digit.
  • print ASCII code for backspace.

(Hint: Store escape sequence for backspace in an integer variable).

Ans(i).

#include<iostream.h>

#include<conio.h>  //for clrscr()

 

//Main Function

void main()

{

int num;

clrscr();  //for clear screen

Read More …

C++ program to compute simple interest and compound interest.

 Q27. Write a program to compute simple interest and compound interest.

Ans.

#include<iostream.h>

#include<math.h>   //for pow()

#include<conio.h>  //for clrscr()

 

//Main Function

void main()

{

float principle,rate,time,n,SI,CI,f;

clrscr();   //for clear screen

 

cout<<“\n Enter Principle Amount: “;

cin>>principle;

cout<<“\n Enter Interest Rate: “; Read More …

C++ program to read two numbers and print quotient and remainder.

Q26. Write a program to read two numbers and print their quotient and remainder.

Ans.

#include<iostream.h>

#include<conio.h>  //for clrscr()

 

//Main Function

void main()

{

int dividend,divisor,remainder,quotient;

clrscr();  //for clear screen

 

cout<<“\n Enter the dividend: “;

cin>>dividend; Read More …

C++ program to convert inches into yards, feet and inches.

Q25. Write a program to convert given inches into its equivalent yards, feet and inches.

(1 yard = 36 inches, 1 foot = 12 inches)

Ans.

#include<iostream.h>

#include<conio.h>  //for clrscr()

 

//Main Function

void main()

{

float inches,feet,yard;

clrscr();  //for clear screen

 

cout<<“\n Given input in inches: “;

cin>>inches;

 

feet=inches/12;

yard=feet/36;

 

cout<<“\n Output in yards: “<<yard<<” yards”;

cout<<“\n Output in feet: “<<feet<<” feet”;

cout<<“\n Output in inches: “<<inches<<” inches”;

 

getch();  //To hold the output screen

}

//End of main()

 

Output:

Given input in inches: 3600

 

Output in yards: 8.333333 yards

Output in feet: 300 feet

Output in inches: 3600 inches

C++ program to accept three digits (that is, 0-9) and print all possible combinations.

Q24. Write a program to accept three digits (that is, 0-9) and print all possible combinations from these digits. (For example, if the three digits are 1, 2 and 3 then all possible combinations are 123, 132, 231, 213, 312 and 321).

Ans.

#include<iostream.h>

#include<conio.h>  //for clrscr();

 

//Main Function

void main()

{

int a[3],i,n=0,swap;

clrscr();  //for clear screen

cout<<“\n Enter three numbers: “; Read More …

C++ program to find even or odd number.

Q23. Write a program to find whether a given number is even or odd.

Ans.

#include<iostream.h>

#include<conio.h>  //for clrscr()

 

//Main function

void main()

{

int n;

clrscr();  //for clear screen

cout<<“\n Enter a number: “; Read More …