Sunteți pe pagina 1din 44

Question: 1

Assignment C++

Q.1: What is string? How they are initialized. Explain NULL terminated strings and illustrate
the purpose of following functions. a) b) c) d) strepy strcmp strlen strcat

Answer:

STRINGS
String is the collection/sequence of characters. In C++ we can work with strings by either using the character array or the predefined string class. In character data type only single of the memory is occupied while in string multiple numbers of bytes of memory are engaged. There are mainly two types of Strings which are commonly used in C++: C-strings and strings that are objects of the string class. We will first discuss first kind of Strings which are arrays of type char. We call these strings C-strings or C-Style strings because they were the only kind of strings available in the C language. Although strings created with the string class, have superseded C-strings in many situations, C-strings are still important for a variety of reasons. First, they are used in many C library functions. Second, they will continue to appear in legacy code for years to come and third C-strings are more primitive and therefore easier to understand on a fundamental level.

(i)

Character Array(C-String):

Character array is the simple array with the data type of char used to hold the strings of characters. To declare C-string variable, as traditionally, we use the data type in front of the C-string variable but the data type must be type char. Then in square bracket the size of the string is initialized. As for each character 1 byte of the memory is engaged but in strings special null character, represented by \0 is appended to the end of the strings to indicate the end of the string. Hence if a string has n characters then it requires an n+1 element array to store it. Thus the character a is stored in a single byte, whereas the single-character string a is stored in two consecutive bytes holding the character a and the null character, which is known as NULL terminated strings.

09CE37

Downloaded From 09ce.blogspot.com

Question: 1

Assignment C++

Initializing the C-string Variable


As other variables and arrays you can also initialize the C-string variable. There are two ways by which you can initialize the C-string variable either you place the string in the double quotation marks in with the equal to sine following the C-string variable or you can place every character of the string enclosed in single quotation marks in the curly braces following the C-string variable and equal to sign. char string[11]=DARYA MEMON; char string[11]={D,A,R,Y,A, , M,E,M,O,N}; Both types of declaration are legal.

(ii) Standard C++ String Class:


As previously we used to store the string in the arrays so it was very complicated, time consuming and stretching. So the standard C++ includes a string class which defines an easy way to store and access the strings. The string class also offers many advantages as you also combine two or more strings. The string class is safer convenient way to store the strings and work with them then the C-strings

Defining/ Initializing the C-string Objects


To define or assign the string object we place the string constant in the double quotation marks followed by the equal to sign after the string object. You can also define string object by placing the string constant in the parenthesis delimited by the double quotation marks. String s2=Darya Memon; Sting s3=(Darya Memon);

Both types of defining are legal.

09CE37

Downloaded From 09ce.blogspot.com

Question: 1

Assignment C++

NULL terminated Strings


In C++, a string is stored as a null-terminated char array. This means that after the last truly usable char there is a null, which is represented in C++ by '\0'. The subscripts used for the array start with zero (0). The following line declares a char array called str. C++ provides fifteen consecutive bytes of memory. Only the first fourteen bytes are usable for character storage, because one must be used for the string-terminating null. char str[15]; The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array. Characters: Subscripts: H 0 e 1 l 2 l 3 o 4 , 5 6 w 7 o 8 r l d ! 9 10 11 12 13 14

a. strcpy( )
strcpy() is used to copy a null-terminated string into a variable. Given the following declarations, several things are posible. Syntax: strcpy(ptr1, ptr2); where ptr1 and ptr2 are pointers to char char S[25]; char D[25];

Putting text into a string: strcpy(S, "This is String 1."); Copying a whole string from S to D: strcpy(D, S); Copying the tail end of string S to D: strcpy(D, &S[8]);

If you fail to ensure that the source string is null-terminated, garbage value may occur.

09CE37

Downloaded From 09ce.blogspot.com

Question: 1

Assignment C++

b. strcmp( )
strcmp() is used to compare two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the point of difference in the first string is less than that in the second a negative value is returned; otherwise, a positive value is returned. Syntax: diff = strcmp(ptr1, ptr2); where diff is an integer and ptr1 and ptr2 are pointers to char

char s1[25] = "pat"; char s2[25] = "pet"; diff will have a negative value after the following statement is executed. diff = strcmp(s1, s2); diff will have a positive value after the following statement is executed. diff = strcmp(s2, s1); diff will have a value of zero (0) after the execution of the following statement, which compares s1 with itself. diff = strcmp(s1, s1);

c. strlen( )
strlen() returns the length of a string, excluding the null. The following code will result in len having the value 13. Syntax: len = strlen(ptr); where len is an integer and ptr is a pointer to char int len; char str[15]; strcpy(str, "Hello, world!"); len = strlen(str);
09CE37

Downloaded From 09ce.blogspot.com

Question: 1

Assignment C++

d. strcat( )
strcat() is used to link together a null-terminated string to end of another string variable. This is equivalent to pasting one string onto the end of another, overwriting the null terminator. There is only one common use for strcat(). Syntax: strcat(ptr1, ptr2); where ptr1 and ptr2 are pointers to char char S[25] = "world!"; char D[25] = "Hello, ";

linking together the whole string S onto D:


strcat(D, S);

09CE37

Downloaded From 09ce.blogspot.com

Question2

Assignment C++

Q.2: Explain Structures? How they can be utilized to define user defined data types?
Answer:

STRUCTURES
Structure is a collection of variables under a single name. Variables can be of any type: int, float, char etc. The main difference between structure and array is that arrays are collections of the same data type and structure is a collection of variables under a single name.

Structures can be utilized to define user defined data types by declaring the structure and defining structure variables. As we know that in C++ everything is defined by the user must be declared so that complier makes sense of it. The declaration provides the blueprint for the compiler, so whenever such things appear in front if the complier it does not worry about it. To declare an structure the keyword struct is used at the starting and the structure name follows. The structure means is also refereed as the tag. Then in the curly braces the variables with their data types are declared and these variables are known as the member of the structure. And at the last the structure is terminated by the semicolon. In this way we can make our own data types and can use in main. The syntax for the simple structure is as, Struct home { int intialcost; char direction; int price; };

We can use structures by defining structure variables, as in above example when the structure is declared now it will behave as the new data type i.e. the structure name as home will be the data type and the variable attached to it will have the data type of type home(having 1 integer and 2 float) to define the structure variable we declare the variables following structure name and you can define as many structure variables you can.

09CE37

Downloaded From 09ce.blogspot.com

Question2

Assignment C++

Syntax: home citizen, defence, qasimabad, latifabad; int plot; As we see above the syntax is same as both are now data type.But int is integer data type but home is also a data type but with multiple data types. The definition of the structure variable set aside the memory for the variable. The amount of memory depends up on the number and type of the members of the structure. In above structure variables citizen, defence, qasimabad, latifabad will occupy 18 bytes of memory ( 4 bytes of int , 10 bytes of char, 4 bytes of int). In this way we can make our own data type with Structures.

09CE37

Downloaded From 09ce.blogspot.com

Question 3

C++ Assignment

Q.3: Write a program that passes an entire structure to a function.


Answer:

PROGRAM
# include<constream.h> # include<iostream.h> void marks(); void marks() { struct pupil { int roll; char gender; int marks; }; pupil number1; cout<<"Enter Roll Number"; cin>>number1.roll; cout<<"\n Enter Gender"; cin>>number1.gender; cout<<"\n Enter Marks"; cin>>number1.marks; cout<<"***************************************************" ; cout<<"\n Roll NUmber:"<<number1.roll;
09CE37

Downloaded From 09ce.blogspot.com

Question 3

C++ Assignment

cout<<"\n Gender:"<<number1.gender; cout<<"\n Marks:"<<number1.marks;

void main() { char cont; clrscr(); marks(); cout<<"\n\n\n... Next? reply with Y= Yes and N=NO"; cin>>cont; if(cont=='y') { marks(); } else getch(); }

OUTPUT

09CE37

Downloaded From 09ce.blogspot.com

Question: 4

Assignment C++

Q.4: Explain Constructor and Destructor? How they are different from functions. Give
reason as to why there can only be a single destructor in a class? Answer:

CONSTRUCTORS
The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.
General Syntax of Constructor
Constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:

<class name> { arguments};


The default constructor for a class X has the form

X::X()
In the above example the arguments is optional.

The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator. There are several forms in which a constructor can take its shape namely:

Default Constructor:
This constructor has no arguments in it. Default Constructor is also called as no argument constructor. For example: class home { private: int a,b; public: home(); }; home :: home() { a=0; b=0; }

09CE37

Downloaded From 09ce.blogspot.com

10

Question: 4

Assignment C++

Copy constructor:
This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. For example to invoke a copy constructor the programmer writes:

home e3(e2) or home e3=e2;

Some important points about constructors:

A constructor takes the same name as the class name. No return type is specified for a constructor. The constructor must be defined in the public. The constructor must be a public member. Overloading of constructors is possible.

DESTRUCTOR
Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.

General Syntax of Destructors

~ classname();
The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

09CE37

Downloaded From 09ce.blogspot.com

11

Question: 4
For example:

Assignment C++

class home{ private: int a; public: home() { } ~ home() { } }

Some important points about destructors:

Destructors take the same name as the class name. Like the constructor, the destructor must also be defined in the public. The destructor must be a public member. The Destructor does not take any argument which means that destructors cannot be overloaded hence there can be only one destructor in class. No return type is specified for destructors.

09CE37

Downloaded From 09ce.blogspot.com

12

Question 5

C++ Assignment

Q.5: How pointers can be used to return more than one value from a function?
Answer:

POINTERS
Pointers are much more commonly used in C++ and in C and In many other languages such as BASIC,PASCAL and Java, which has no pointers. Pointers provide an essential tool for increasing C++ programming. A notable example is the creation of data structures such as linked lists and binary tests. The ideas behind the pointers are not complicated. Every byte in the computers memory has an address. Address are numbers, just are for houses on a street. The numbers start at 0 and go up from there and so on. If you have 1 MB of memory the highest address is 1,048,575. Your program when it is loaded into memory occupies a certain range of these addresses. That mean that every variable and every function in your program starts at a particular address. And the address of a variable is not at all same as its contents. C++ allows operations with pointers to functions. The typical use of this is for passing a function as an argument to another function, since these cannot be passed dereferenced. In order to declare a pointer to a function we have to declare it like the prototype of the function except that the name of the function is enclosed between parentheses () and an asterisk (*) is inserted before the name:

// pointer to functions #include <iostream> using namespace std; int addition (int a, int b) { return (a+b); } int subtraction (int a, int b) { return (a-b); } int operation (int x, int y, int (*functocall)(int,int)) { int g; g = (*functocall)(x,y); return (g); } int main () { int m,n; int (*minus)(int,int) = subtraction; m = operation (7, 5, addition); n = operation (20, m, minus); cout <<n; return 0; }

09CE37

Downloaded From 09ce.blogspot.com

13

Question 6

C++ Assignment

Q.6: Explain Loops? Write the basic strucuture of FOR, WHILE, and DO-WHILE Loop. Explain
the key difference between a while and do-while loop. Answer:

LOOPS
In object-oriented programming language, whenever a block of statements has to be repeated a certain number of times or repeated until a condition becomes satisfied, the concept of looping is used. The loops cause a section of your program to be repeated a certain number of times. The repetition continues while a condition is true. When the condition becomes false the loop ends and control passes to the statement following the loop.

for loop:
The syntax of for loop is

for (initialization; condition; increase or decrease) { statement block; } Initialization is the primary value set for a variable. Initialization is only executed one time. After initialization is performed, it is not executed again. The condition specified is checked and if the condition returns a value of true, (if the condition is satisfied) the loop is continued for execution. If the condition returns a value of false (the condition is not satisfied), then the statements following the loop block are not executed and the control is transferred to the end of the loop. The statement specified in the for loop can be a statement block or more than one statement. These statements must be enclosed within flower brace symbols { }. The statement block can also be a single statement. For a single statement, the flower brace symbols are not needed and may be specified as: for (initialization; condition; increase or decrease) statement;

If the condition becomes satisfied, all the statements in for block are executed; the increase or decrease is executed on the variable and gain. The condition is then checked and the loop continues with these commands until the condition becomes false.

09CE37

Downloaded From 09ce.blogspot.com

14

Question 6

C++ Assignment

It is important for the programmer to remember that the optional attributes are initialization and increase/decrease where the for loop must be written as: for (;condition;) or for(initialization ; condition;)

It is also possible to initialize more than one variable in a for loop and to perform this the programmer uses the comma operator denoted as ,
For example:

To initialize two variables namely a and b with value 5 and 10 respectively and increment a and decrement b this can be done as follows; for (a=5,b=10;a!=b; a++, b--)
small example using for loop:

#include <iostream.h> void main() { for (int x=5; x>0; x--) { cout << x << "; "; } cout << "Darya"; } The output of this program is 5; 4; 3; 2; 1; Darya

09CE37

Downloaded From 09ce.blogspot.com

15

Question 6

C++ Assignment

While loop:
Statement of while statement is: while (expression) { statement block; }

Until the condition represented in the expression is true (satisfied), the set of statements in while block is executed. As in for loop, when the number of statements to be executed is more than one (if the condition is satisfied), then it is placed inside the flower braces as shown in the example above. If the number of statements to be executed is one (if the condition is satisfied), then the flower braces may be removed.
For Example:

#include <iostream.h> void main () { int x; cout << "Input the number:"; cin >> x; while (x>0) { cout << x << "; "; --x; } cout << "Darya"; }

Here the output of the above program is

Input the number: 5 5; 4; 3; 2; 1; Darya

In this example, the input number is 5 and the while loop is executed starting from 5 until the number reaches a value <0. The value of x>0. The statement inside the while block prints the value of x. When this is completed, the numbers 5, 4, 3, 2, 1 are printed. After the value of x reaches less than zero, the control passes to outside the while block and Darya is printed.
09CE37

Downloaded From 09ce.blogspot.com

16

Question 6

C++ Assignment

Do-while loop:
The syntax of do-while loop is

do { statement block; } while (condition);

The do while loop functionality is similar to while loop. The main difference in dowhile loop is that the condition is checked after the statement is executed. In do-while loop, even if the condition is not satisfied the statement block is executed once.
For Example:

include <iostream.h> void main () { int x; do { cout << "Input the number:"; cin >> x; cout << "The number is:" << x << "\n"; } while (x != 5); cout << End of program; }

The output of the above program is

Input the number: 10 The number is: 10 Input the number: 5 End of program

09CE37

Downloaded From 09ce.blogspot.com

17

Question 6

C++ Assignment

KEY DIFFERENCE

Test Expression

False

EXIT

Body of Loop

True
Test Expression

Body of Loop

EXIT
False

True

Operation of While Loop

Operation of Do-while Loop

As we can see clearly in While loop first we set test expression then the body of loop is executed but in Do-while we first execute the body then the text expression means in Do-while program has to execute once then it depends upon text expression to move on or to stop.

09CE37

Downloaded From 09ce.blogspot.com

18

Question 7

C++ Assignment

Q.7: State the difference between life and scope of variable with the help of example. Explain local,
global and static variable.

Answer:

SCOPE OF VARIABLES
All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, at the beginning of the body of the function main when we declare a, b, and result were of type int or in the function. A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function. In the example above, this means that if another function existed in addition to main, the local variables declared in main could not be accessed from the other function and vice versa.

A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.

09CE37

Downloaded From 09ce.blogspot.com

19

Question 7

C++ Assignment

When a local static variable is created, it should be assigned an initial value. If it's not, the value will default to 0.The following example sets a static local, called TheLocal, to the value 1000. void main(void){ static int TheLocal; TheLocal=1000; } /* a static local variable*/

A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope The following example also sets a global, called TheGlobal, to the value 1000 static int TheGlobal; / * a static global variable*/ void main(void){ TheGlobal=1000; }

09CE37

Downloaded From 09ce.blogspot.com

20

Question 8

C++ Assignment

Q.8:

Write a class that depicts a circular which includes the following functions.

A- Add, subtract, multiply and divide. B- Square and Square root. C- Cosine, Sine and Log Function. Answer:

PRORGAM
# include<constream.h> #include<math.h> #include<iomanip.h> Class calculator { Private: Float a, b, r; Public: Calculator() { r=0 cout<<setw(33)<<calculator<<endl; } Void add(void) { Cout<<Enter two number<<endl;

09CE37

Downloaded From 09ce.blogspot.com

Question 8

C++ Assignment

Cin>>a>>b; R=a+b; Cout<<Addition=<r; } Void sub(void) { Cout<<endl<<Enter two number<<endl; Cin>>a-b; r=a-b; cout<< Subtraction = <<r; } Void multi () { Cout<<Enter two numbers<<endl; Cin>>a>>b; R=a/b; Cout<<division=<<a/b; } Void sine() { Cout<<Enter the angle in radian<<endl; Cin>>a; R=sin(a); Cout<<sin(a); }

09CE37

Downloaded From 09ce.blogspot.com

ii

Question 8

C++ Assignment

Void cosine() { Cout<<Enter the angle in Radian<<endl; Cin>>b; r=cos(b); cout<<cos(b); } Void logarithm() { Cout<<Enter the value of log<<endl; Cin<<a; R=log10(a); } Void square() { Cout<<Enter the number<<endl; Cin>>a; R=pow(a,2); Cout<<Sqaure of :<<a<<is<<pow(9.0.5); } Void result() { Cout<<r; } Calculaor()

09CE37

Downloaded From 09ce.blogspot.com

iii

Question 8

C++ Assignment

{ Cout<< thanks you for using calculator<<endl; } }; Void main(void) { Int a; Char b,x; Clrscr(); { Calculator(); For (a=0,1a<3) { Clrscr(); Cout<<setw(35)<<calculator<<endl; Cout<<press following key to operate different function<<endl; Count>>a for addition s for subtraction and m for multiplication<<endl; Cout<<D for division S for square and R for square root<<endl; Cout<<L for logarithm and C for Cosine<<endl; Cout<<last result=; C1.result(); Cout<<endl; Cin>>x; Switch(x) {

09CE37

Downloaded From 09ce.blogspot.com

iv

Question 8

C++ Assignment

Case a: C1.add(); Breaku: Casem C1.multi(); Break: Cases C1.sub(); Break: Cased C1.divison(); Break: Casef C1.squareroot(); Break: CaseI C1.logarithm(); Break: Casee C1.cosine(); Break: Casen C1.sine(); Break: Default:

09CE37

Downloaded From 09ce.blogspot.com

Question 8

C++ Assignment

Cout<<Wrong arithmetic operater<<endl; Break: } Cout<<\n for cosine press y and for exit press n<<endl; Cin>>b; If ( b==n) { A++ } } } Getch(); }

09CE37

Downloaded From 09ce.blogspot.com

vi

QUESTION 9

C++ Assignemnt

Q.9: Write a program that prints Diamond on he Console.


Answer:

PROGRRAM
# include<constream.h> # include<iomanip.h> int i=0; void main()

{ clrscr(); int blanks=40,size=8; for(i=1 ;i<size ;i+=2) { cout<<setw(blanks); for(int j=0; j<i; j++) cout<<"*"; cout<<endl; --blanks; } for(i=size; i>1; i-=2) { cout<<setw(blanks); for( int j=i; j>0; j--) cout<<"*";

09CE37

Downloaded From 09ce.blogspot.com

21

QUESTION 9

C++ Assignemnt

cout<<endl; ++blanks; } getch(); }

OUTPUT

09CE37

Downloaded From 09ce.blogspot.com

22

Question 10

C++ assignemnt

Q.10(A):
Answer:

A Program that generates faconic series up to (1 1 2 3 5 8 12 21..)

PRORGAM
#include<constream.h> #include<iostream.h> void main() { clrscr(); unsigned long limit=4294967295; unsigned long next=0; unsigned long last=1; while( next<limit /2) { cout<<last<<" "; long sum=next+last ; next=last; last=sum; } cout<<endl; getch(); }

09CE37

Downloaded From 09ce.blogspot.com

23

Question 10

C++ assignemnt

OUTPUT

09CE37

Downloaded From 09ce.blogspot.com

24

Question 10

C++ assignemnt

Q.10(B):
Answer:

) A program that determine whether a number is prime or not ?

PRORGAM
#include<constream.h> void main() { int n;

clrscr(); cout<<"Enter number"; cin>>n; for (int j=2; j<=n/2;j++) if ( n%j ==0 ) { cout<<"its not prime"; break; cout<<"its a prime\n "; } getch(); }

09CE37

Downloaded From 09ce.blogspot.com

25

Question 10

C++ assignemnt

OUTPUT

09CE37

Downloaded From 09ce.blogspot.com

26

Question 10

C++ assignemnt

Q.10(C):
Answer:

) A program that tells the factorial of numbers ?

PRORGAM

#include<constream.h> void main() { clrscr(); int x,i; cout<<"Enter Number:"; cin>>x; int fact=1; for(i=x; i>=1; i--) fact=fact*i; cout<<"The factorial is: "<<x<<"="<<fact; getch(); }

09CE37

Downloaded From 09ce.blogspot.com

27

Question 10

C++ assignemnt

OUTPUT

09CE37

Downloaded From 09ce.blogspot.com

28

Question 12

C++ assignment

Q.12: What is object oriented programming? Write its advantage and characteristics in detail.
What we mean by the term Data Encapulation.

Answer:

Object Oriented Programming


Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. Programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s. Many modern programming languages now support OOP.

Object-oriented programming has roots that can be traced to the 1960s. As hardware and software became increasingly complex, quality was often compromised. Researchers studied ways to maintain software quality and developed object-oriented programming in part to address common problems by strongly emphasizing discrete, reusable units of programming logic. The methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) each containing all the information needed to manipulate its own data structure. This is in contrast to the existing modular programming which had been dominant for many years that focused on the function of a module, rather than specifically the data, but equally provided for code reuse, and self-sufficient reusable units of programming logic, enabling collaboration through the use of linked modules (subroutines). This more conventional approach, which still persists, tends to consider data and behavior separately.

An object-oriented program may thus be viewed as a collection of cooperating objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects and can be viewed as an independent 'machine' with a distinct role or responsibility. The actions (or "operators") on these objects are closely associated with the object. For example, the data structures tend to carry their own operators around with them (or at least "inherit" them from a similar object or class).

09CE37

Downloaded From 09ce.blogspot.com

32

Question 12

C++ assignment

Data Encapsulation
Encapsulation is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions present inside the class. Data encapsulation led to the important concept of data hiding. Data hiding is the implementation details of a class that are hidden from the user. The concept of restricted access led programmers to write specialized functions or methods for performing the operations on hidden members of the class. Attention must be paid to ensure that the class is designed properly.

Neither too much access nor too much control must be placed on the operations in order to make the class user friendly. Hiding the implementation details and providing restrictive access leads to the concept of abstract data type. Encapsulation leads to the concept of data hiding, but the concept of encapsulation must not be restricted to information hiding. Encapsulation clearly represents the ability to bundle related data and functionality within a single, autonomous entity called a class. For instance:

class 09ce { public: int sample(); int example(char *se) int endfunc(); ......... ......... //Other member functions private: int x; float sq; .......... ......... //Other data members }; In the above example, the data members integer x, float sq and other data members and member functions sample(),example(char* se),endfunc() and other member functions are bundled and put inside a single autonomous entity called class Exforsys. This exemplifies the concept of Encapsulation. This special feature is available in object-oriented language C++ but not available in procedural language C. There are advantages of using this encapsulated approach in C++. One advantage is that it reduces human errors. The data and functions bundled inside the class take total control of maintenance and thus human errors are reduced. It is clear from the above example that the encapsulated objects act as a black box for other parts of the program through interaction. Although encapsulated objects provide functionality, the calling objects will not know the implementation details. This enhances the security of the application. The key strength behind Data Encapsulation in C++ is that the keywords or the access specifiers can be placed in the class declaration as public, protected or private. A class

09CE37

Downloaded From 09ce.blogspot.com

33

Question 12

C++ assignment

placed after the keyword public is accessible to all the users of the class. The elements placed after the keyword private are accessible only to the methods of the class. In between the public and the private access specifiers, there exists the protected access specifier. Elements placed after the keyword protected are accessible only to the methods of the class or classes derived from that class. The concept of encapsulation shows that a non-member function cannot access an object's private or protected data. This adds security, but in some cases the programmer might require an unrelated function to operate on an object of two different classes. The programmer is then able to utilize the concept of friend functions. Encapsulation alone is a powerful feature that leads to information hiding, abstract data type and friend functions.

09CE37

Downloaded From 09ce.blogspot.com

34

Question 13

C++ assignment

Q.13: State the difference between life and scope of variable with the help of example. Explain
local, global and static variable.

Answer:

ESCAPE SEQUENCES (Backslash Character Constants)

Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character. Given below is the table of escape sequence and their meanings. Constant '\a' '\b' '\f' '\n' '\r' '\t' '\v' '\'' '\"' '\?' '\\' '\0' Meaning

.Audible Alert (Bell) .Backspace

.Formfeed .New Line .Carriage Return .Horizontal tab .Vertical Tab .Single Quote .Double Quote .Question Mark .Back Slash .Null

09CE37

Downloaded From 09ce.blogspot.com

34

Question 13

C++ assignment

09CE37

Downloaded From 09ce.blogspot.com

35

Question 14

C++ Assignment

Q.14:
Answer:

Write a program which passes an array to a function.

PRORGAM
#include<constream.h> void program(); void main() { clrscr(); program(); getch(); }

void program() { const int size=5; int a[size]; cout<<"Enter"<<size<<"numbers"; for( int i=0;i<size;i++) cin>>a[i]; for( int j=0;j<size;j++) cout<<a[j]<<endl; }

09CE37

Downloaded From 09ce.blogspot.com

36

Question 14

C++ Assignment

OUTPUT

09CE37

Downloaded From 09ce.blogspot.com

37

Question 15

C++ Assignment

Q.15:
Answer:

PRORGAM
#include<constream.h> #include<studio.h>

void main() { clrscr(); int loop; int element[6]={0,1,2,3,4,5}, value[b]={10,14,5,89,3}=loop,loop1; cout<<element 1+1t value 1+1+1 histrogram \n; for(loop=0; loop<=5; loop++) { Cout<<\n \n <<element[loop]<<value[loop]<<1+1; For (loop1=1 ;loop1<=value[loop];loop1++) Cout<<*; } Getch(); }

09CE37

Downloaded From 09ce.blogspot.com

38

Question 16

C++ Assignment

Q.16:
Answer:

PRORGAM
#include<constream.h> #include<studio.h> #include<math.h>

void main() float marks[10][3],ar[3]par[10]; float [10]; char grade[10] int a,b=0; for (a=1;a<=10;a++)

{ clrscr(); cout<<\n enter the marks of students<<a<<for three subjects; cin>>marks[a][b]>>marks[a][b+1]>>marks[a][a+2]; } For(b=0;b<=3;b++) Avg[b]+=marks[a][b];avg[b] } For(a=1;a<=10;a++) { 09CE37

Downloaded From 09ce.blogspot.com

37

Question 16 For b=0;b<3;b++) Total=[a]+=marks[a][b];per[a]=total[a]/300*100; If (per[a]==70) Grade[a]=A; Where if (per[a]>60&&per[a]<70) Grade[a]=B Else if ( per[a]<60) Grade==c; } Clrscr(); B=0 Cout<<in average marks in maths in complex variable and in computer ; Cout<<n<<avg[b]<<\n<<abc[b+1]<<\n<<abg[b+1]; Cout<<total marks of each student with percentage and grade; Cout<<***************; Cout<<\n<<grade; Getch(); }

C++ Assignment

09CE37

Downloaded From 09ce.blogspot.com

38

S-ar putea să vă placă și