Вы находитесь на странице: 1из 11

Question 1 a) Polymorphism polymorphism enables us to write programs that process objects that share the same superclass in a class

hierarchy as if they are all objects of the superclass. A variable with a given name may be allowed to have different forms and the program can determine which form of the variable to use at the time of execution. For example, a variable named USERID may be capable of being either an integer (whole number) or a string of characters (perhaps because the programmer wants to allow a user to enter a user ID as either an employee number - an integer - or with a name - a string of characters). By giving the program a way to distinguish which form is being handled in each case, either kind can be recognized and handled. b) Inheritance Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:. Variable Variables are simply names used to refer to some location in memory a location that holds a value with which we are working.It may help to think of variables as a placeholder

Identifierj r names given to program elements such as variables , arrays & functions. Basically identifers r the sequences of alphabets or digits. an identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline [2] Class A class is a mechanism for creating user-defined data types. a class is composed of a set of data members and a set of operations that can be performed on the class. In C++, a class type can be declared with the keywords union, struct, or class. A union object can hold any one of a set of named members. Structure and class objects hold a complete set of members. Each class type represents a unique set of class members including data members, member functions, and other type names. The default access for members depends on the class key: class X { /* define class members here */

}; int main() { X xobject1; X xobject2; // create an object of class type X // create another object of class type X [2]

Object the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit. A region of storage with associated semantics. After the declaration int i; we say that "i is an object of type int." In OO/C++, "object" usually means "an instance of a class." Thus a class defines the behavior of possibly many objects (instances). [2] Data Hiding Data Hiding is also known as Encapsulation. Data Hiding is the mechanism where the details of the class are hidden from the user.The user can perform only a restricted set of operations in the hidden member of the class.Encapsulation is a powerful feature that leads to information hiding,abstract data type and friend function.They encapsulate all the essential properties of the object that are to be created.Using the method of encapsulation the programmer cannot directly access the class

class class name{ private: datatype data; public: Member functions; }; main(){ classname objectname1,objectname2; } Abstract Data Type (ADT) Abstract Data Type are data-type that combines the common functionalities from different related objects into one package, so that those different but related object's interface can inherit from the

ADT thus making it more flexible and and less coding for the programmer. WHERE: Anywhere, when you code, in your life wherever applicable. WHEN : Use it when you want to generalize a design. When you have many objects that have similar functionalities, you can Abstract the common functionalities into one class, and have the other objects inherit from them. WHY You would create ADT for numerous reasons. First it generally leads to a flexible design if designed well. Second its less coding for the programmer. Third its more reuseability . Fourth, it leads to easier maintenance. And Fifth, it creates the possibility of polymorphism int main( int argc, char *argv[]) { // creat int stack std::stack<int> i; i.push(1); i.push(3); i.push(5); // create float stack std::stack<float> f; f.push(2.9884); f.push(4.8885); f.push(6.444); // show int stack while( !i.empty() ){ . std::cout << i.top() << std::endl; i.pop(); } // show float stack while( !f.empty() ) { std::cout << f.top() << std::endl; }. return 0; } Early Binding Also called static binding, means the compiler is able to directly associate the identifier name (such as a function or variable name) with a machine address. All functions have a unique machine address. So when the compiler encounters a function call, it replaces the function call with a machine language instruction that tells the CPU to jump to the address of the function. Example f.pop();

void Print(int n) { std::cout << n; }

int main() { Print (6); return 0; } Late Binding In some programs, it is not possible to know which function will be called until runtime (when the program is run). This is known as late binding (or dynamic binding). In C++, one way to get late binding is to use function pointers. A function pointer is a type of pointer that points to a function instead of a variable. The function that a function pointer points to can be called by using the function call operator (()) on the pointer int Add(int nX, int nY) { { return nX + nY; } int main()

// Create a function pointer and make it point to the Add function

int (*pFcn)(int, int) = Add; cout << pFcn(5, 3) << endl; // add 5 + 3 return 0; Data Type Data types refers to an extensive system for declaring variables of different types. There are several integral data types, a character data type, floating point data types for holding real numbers and more. In addition you can define your own data types using aggregations of the native types. E. g int x, float y , char z

Question 2

a)

What is the difference between the constants 5, 5 and 5

5 is a constant. 5 is a character 5 is a string b) What do the following loops print

for (i = 0; i < 10; i = i + 2 ) cout<<i<<\n; prints: 2, 3,4,5,6,7,8,9

for (i = 100; i >= 0; i = i - 7) cout<<i<<\n; prints: 93, 86, 79, 72, 65, 58, 51, 44, 37, 30 , 23, 16, 9, 2

for (i = 1; i <= 10; i = i + 1) cout<<i<<\n; Prints: 2, 3, 4, 5, 6, 7, 8, 9, 10

for (i = 2; i < 100; i = i * 2) cout<<I; cout<<\n;

It will not print anything because there is an error c) Write a C++ program that print a 10 * 10 multiplication table

#include<iostream> using namespace std; int main () { int i, j;

for(i=1;i<=10;i++){ for(j=1;j<=10;j++){ cout<<"%d x %d = %d\n"<< i, j, i * j; } cout<<"\n"; } getchar (); // getch(); return 0; } d) Write a C++ program that will printout all the rotations of a string passed to it. For example if a user enters RICHIE, the output will be as follows: RICHIE ICHIER CHIERI HIERIC IERICH ERICHI #include<iostream> using namespace std; int main() { char x[20]; int i, j, n,l=0; cout<<"Enter a string"; cin>>x; n=strlen(x); for(i=0;i<n;i++) { l=i;koio for(j=0;j<n;j++) cout<<x[l++%n]; cout<<endl; } return 0; getchar(); }

e) Write a program to print the numbers between 1 and 10, along with an indication of whether each is odd or even, like the following (Use the For & If Statement) 1 is odd 2 is even 3 is odd #include<iostream> using namespace std; int main() { int a; for (a=1; a<=10;a++) { if(a%2 != 0) { cout<<"\nThe Number %d Is Odd"<<a; } else if(a%2 == 0) { cout<<"\nThe Number %d Is Even"<<a; } } getchar(); } f) Write a C++ Program that reads the marks obtained of ten students out of 100. It should also computes the average, lowest and highest marks. The program should also show the difference of marks of every student from the average marks #include<iostream> using namespase std; float average_marks(float []); float highest_marks(float []); float lowest_marks(float []); int main() {

float marks[10]={0}; cout<<"\n Enter the marks of the ten students : "<<endl;

cout<<"\n\t Student No.

Marks Obtained"<<endl;

for(int count_1=0;count_1<10;count_1++) { cout<<"\t\t"<<count_1+1<<"\t\t"; cin>>marks[count_1]; } getch();

cout<<"\n ******************** Result Sheet ****************"<<endl; cout<<"\n Total Marks = 100"<<endl; cout<<" Average Marks = "<<average_marks(marks)<<endl; cout<<" Highest Marks obtained = "<<highest_marks(marks)<<endl; cout<<" Lowest Marks obtained = "<<lowest_marks(marks)<<endl; cout<<"\n Student No. Marks Obtained Diff. from Average Marks Status"<<endl;

int y=10; for(int count_2=0;count_2<10;count_2++) { cout<<"\t"<<count_2+1<<"\t\t"<<marks[count_2]; gotoxy(42,y); cout<<marks[count_2]-average_marks(marks)<<"\t\t "<< ((marks[count_2]>=50)?"Pass":"Fail")<<endl; y++; } getch(); return 0; }

// average marks calculation// { float sum=0; for(int count=0;count<10;count++) sum+=marks[count]; return sum/10; }

// highest marks// { float highest=marks[0]; for(int count=0;count<10;count++) { if(marks[count]>highest) highest=marks[count]; } return highest; } //lowest marks// { float lowest=marks[0]; for(int count=0;count<10;count++) { if(marks[count]<lowest) lowest=marks[count]; } return lowest; } return 0; getchar (); }

g)

Write a C++ that reads marks obtained by a student in a test of 100 marks and computes his/ her grade according to the following criteria. For marks >= 80 grade = A, marks >= 70 & < 80 grade = B, marks >= 60 & < 70 grade = C, marks >= 50 & < 60 grade = D, otherwise grade = F [5

#include<iostream> using namespace std;

main( ) {

int marks; char grade; int student;

cout<<"\n Enter your Marks = "; cin>>marks;

switch (student) { case 1: (marks>=80 && marks<=100); grade='A'; break;

case 2: (marks>=70 && marks<80); grade='B'; break;

case 3:(marks>=60 && marks<70); grade='C'; break;

case 4:(marks>=50 && marks<60); grade='D'; break; default: grade='F'; break;

} cout<<"\n Your Grade is = "<<grade<<endl; getchar( ); return 0; } h) Give an example of a function prototype and identify its part Int maximum (int, int,int) It has function name, parameters and function type. It takes in 3 integers and returnas an integer. i) What will be the values of the variable a in the following code segments: i) int b= 4; int a; a=++b; a+=a; 10 int b = 4; int a; a=b++; a=a+1; 5 [4]

Answer ii)

Answer

j) How many differences can you think of between i and J as defined by the following two lines? int i = 10; #define J 10 -In J everything to right of identifier replaces text ie J is replaced by 10 WHEREAS in i, i is assigned the number 10. - J cannot be redefined once created WHEREAS i can be changed over and over again ; i is only a place holder -J is a symbolic constant WHEREAS i is a variable

Вам также может понравиться