You are on page 1of 13
IV: Pointers and polymorphism in C++ 1. Define pointer. Give syntax of declaration and initialization of pointer. = A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. Declaration: datatype *pointer_name; Intialization: inta= 10; int*ptr;_//pointer declaration ptr=&a; —_//pointer initialization 2. Explain pointer arithmetic with example. © The C++ language allows you to perform integer addition or subtraction operations on pointers. If ptr points to an integer, ptr + 1 is the address of the next integer in memory after ptr. ptr - 1 is the address of the previous integer before pir . 3. Write a program to display elements of array using pointer to array of integers? © #include using namespace std: int main() { int arr[5] = { 1,234, 5}; int *ptr = arr; cout <<"\n"<< ptr; return 0; . Differentiate call by value and call by reference with suitable example © Call by Value is a method in which it passes the copies of actual parameters to the formal parameters inside the function. And as you know, it passes the copies, so even if there is any change in the values inside the function, it will not reflect that change in the actual values. Call by Reference is a method in which it passes the reference or address of the actual parameter to the function's formal parameters, which means if there is any change in the values inside the function, it reflects that change in the actual values. . Write a program to concatenate two strings using pointer to strings. > #include using namespace std ; void concat(char* str , char* str2) { while(*str1 != '\0') { } while(*str2 != \0') { } *strl = 0; *strl ++ 5 *otrl++ = *er21+ ; int main () { char “fn = "Raman char “In = "Ghai"; char *fullname = fn ; concat(fn,In); cout <<"\nAfter concat:\n"<<"Strl: "< #include Using namespace std; int main() { char ch, str[50]5 int i-@, flag=0; cout<<“\n Enter a String:”; gets(str); cout<<"\nEnter a character to search in a string:”5 cin>>ch3 while(str[i]!="\@") tr[i]) cout<<\ncharacter is found at< #include using namespace std; int main() { char *str="ForgetCode"; cout<<"Reverse the Siring:"; for(int i=(strlen(str)-1);i>=0: { cout< using namespace std; int string_length(char* given_string) { int length = 0; while (*given_string { Length++; given_string++; } return length; } int main) { char given_string[] = "geeksforgeeks"; cout << string_length(given_string); return 0; } 9. Explain constructor overloading with suitable example. as function overloading. Overloaded constructors have the same name (name of the class) but the different number of arguments. Depending upon the number and type of arguments passed, the corresponding constructor is called. 10. Write program using function overloading to swap two integers and swap two float number. > #include #include #include class Check { public: swap function with int type arguments void swap(int x,int y) { cout<<"\nBefore swapping integer values\nValue 1 = "< using namespace std class Vehicle { public: void body () { } cout<<"\n could be metallic or non-metallic body\n"; h class Car: public Vehicle { public: void body () { } cout<<"\n metallic body\n"; class bike: public Vehicle public: void body () { cout<<"\n non-metallic body\n"; } int main () { car cs bike b; Vehicle * vi Vehicle * v2 vi->body() 5 v2->body()} return 0; 20.Explain abstract class with suitable example. © An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0 ) in the declaration of a virtual member function in the class declaration, 21.Explain virtual base class with suitable example. = When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. 22. What is this pointer explain with examples => A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable. 23.Write a program to demonstrate the use of this pointer. #includeciostream> using namespace std; class Test { private: int x; public: void setx (int x) c this->x = x; + void print() { cout << "x = " <« x << endl; a int main() { Test obj; int x = 20; obj .setx(x); obj.print(); return @; 24. Explain how pointer is assigned to object. Give suitable example. © Just like Other Pointers, the object pointers are declared by placing in front of a object pointer’s name. The Syntax is: class_name * Object_pointer_name; In above Syntax, class_Name is the name of an already defined class and object_pointer_name is the pointer to an object of this class type. 25.Write a program to declare class city with data members city_name and state. Create array of object of size 5. Read and print data for array using pointer to object. #include #include class city { char city_name[20],state[20}; public void accept() { cout<<"\nEnter city data:"; cin>>city_name; cout<<"\nState: cin>>state; } void display() { cout<<"\nCity data is:"; coutc<"\nName:"<accept() ptr->displayO; getch0; } 201F235 Darshan Pawar

You might also like