Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
31 views
13 pages
Assignment No.04 (OOP)
Object oriented programing in c++ assignment 4
Uploaded by
suraj Patil
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Assignment No.04(OOP) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
31 views
13 pages
Assignment No.04 (OOP)
Object oriented programing in c++ assignment 4
Uploaded by
suraj Patil
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Assignment No.04(OOP) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 13
Search
Fullscreen
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 = "<
In C++, following are the general rules for operator overloading. 1) Only built-in operators can be overloaded. New operators can not be created. 2) Arity of the operators cannot be changed. 3) Precedence and associativity of the operators cannot be changed. 4) Overloaded operators cannot have default arguments except the function call operator () which can have default arguments. 5) Operators cannot be overloaded for built in types only. At least one operand must be used defined type. 6) Assignment (=), subscript ([]), function call (“()”), and member selection (- >) operators must be defined as member functions 7) Except the operators specified in point 6, all other operators can be either member functions or a non member functions. 8) Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.13. Write a program to overload operator ‘+’ to add two complex numbers. #include
#include
#include
using namespace std; class Complex { private: int real, imag; public: Complex() { } Complex (int, int i) { real = imag = 0; real = imag = i; } string to_string() stringstream ss; iftimag >= 0) ss <<"(" << real <<" +" << imag <<"i)"; else ss <<"(" << real <<" - "<< abs(imag) << "i)"; return ss.str(; } Complex operator-+(Complex ¢2){ Complex ret; ret.real = real + c2.real; ret.imag = imag + c2.imag; return ret;int main() { Complex c1(8,-5), ¢2(2,3); Complex res = cl + c2; cout << res.to_string(); } 14.Explain polymorphism. © Polymorphism is the ability of a programming language to present the same interface for several different underlying data types. Polymorphism is the ability of different objects to respond in a unique way to the same message. 15.Differentiate between run time polymorphism and compile time polymorphism. 2 Sr.No | Compile Time Polymorphism 1 In Compile time Polymorphism, the call is resolved by the compiler. Run time Polymorphism In Run time Polymorphism, the call is not resolved by the compiler. Itis also known as Dynamic Itis also known as Static binding, Early binding and overloading as well binding, Late binding and overriding as well Method overloading is the compile- time polymorphism where more than one methods share the same name with different parameters or signature and different retumn type. Method overriding is the runtime polymorphism having same method with same parameters or signature, but associated in different classes. Itis achieved by function overloading and operator overloading. Itis achieved by virtual functions and pointers. 16.Explain virtual function. Give suitable example © A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function's declaration in the base class with the keyword virtual.17.Explain function overloading with example © Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfunen(int a, float b) is (int, float) which is different from the function myfunen(float a, int b) parameter list (float, int). Function overloading is a compile-time polymorphism. 18.Explain pure virtual function with suitable function. © A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don’t know about the derived class The derived class can either fully replace (“override”) the base class member function, or the derived class can partially replace (“augment”) the base class member function. The latter is accomplished by having the derived class member function call the base class member function, if desired. 19. Write a program to demonstrate dynamic binding using virtual function. ° #includeciostream> 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 Vehiclepublic: 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
C++ Polymorphism
PDF
100% (1)
C++ Polymorphism
17 pages
Polymorphism in C
PDF
100% (1)
Polymorphism in C
8 pages
Oop Unit Iv
PDF
No ratings yet
Oop Unit Iv
32 pages
C Answers
PDF
No ratings yet
C Answers
27 pages
Polymorphism in C
PDF
No ratings yet
Polymorphism in C
8 pages
Polymorphism in C++
PDF
100% (1)
Polymorphism in C++
8 pages
C++ Polymorphism
PDF
100% (1)
C++ Polymorphism
1 page
Polymorphism in C Best New
PDF
100% (1)
Polymorphism in C Best New
10 pages
Unit-III Oop RBK
PDF
No ratings yet
Unit-III Oop RBK
105 pages
Unit 5
PDF
No ratings yet
Unit 5
72 pages
Polymorphism
PDF
No ratings yet
Polymorphism
20 pages
Provided by Mayur K
PDF
No ratings yet
Provided by Mayur K
35 pages
Unit - 5
PDF
No ratings yet
Unit - 5
33 pages
Vimp Oop
PDF
No ratings yet
Vimp Oop
57 pages
Unit 3
PDF
No ratings yet
Unit 3
59 pages
Chapter4pdf 2024 10 09 10 02 35
PDF
No ratings yet
Chapter4pdf 2024 10 09 10 02 35
31 pages
Polymorphism - MCA
PDF
No ratings yet
Polymorphism - MCA
26 pages
Oops 3
PDF
No ratings yet
Oops 3
42 pages
Ilovepdf Merged
PDF
No ratings yet
Ilovepdf Merged
22 pages
Poly C++
PDF
No ratings yet
Poly C++
32 pages
Oop
PDF
No ratings yet
Oop
40 pages
Pointer and Polymorphism in C++
PDF
No ratings yet
Pointer and Polymorphism in C++
23 pages
Apr 2023 B.B.A (C.a) 2019 Pattern - CPP - Solutions
PDF
No ratings yet
Apr 2023 B.B.A (C.a) 2019 Pattern - CPP - Solutions
17 pages
Oop Anser ct2 Riya (1) - 1
PDF
No ratings yet
Oop Anser ct2 Riya (1) - 1
16 pages
POLYMORPHISM
PDF
No ratings yet
POLYMORPHISM
28 pages
Polymorphism (Virtual Function and Functon Overloaing)
PDF
No ratings yet
Polymorphism (Virtual Function and Functon Overloaing)
29 pages
Mock Test
PDF
No ratings yet
Mock Test
29 pages
OOP Unit 4 Notes (Part 2)
PDF
No ratings yet
OOP Unit 4 Notes (Part 2)
14 pages
Polymorphism (1) 1
PDF
No ratings yet
Polymorphism (1) 1
22 pages
C++ Interview Question
PDF
No ratings yet
C++ Interview Question
17 pages
Unit 3
PDF
No ratings yet
Unit 3
22 pages
Unit 4-Polymorphism
PDF
No ratings yet
Unit 4-Polymorphism
17 pages
Chapter 3 (Polymorphism)
PDF
No ratings yet
Chapter 3 (Polymorphism)
17 pages
304 OOPS Unit - 3
PDF
No ratings yet
304 OOPS Unit - 3
18 pages
Polymorph Is M
PDF
No ratings yet
Polymorph Is M
21 pages
Unit 4
PDF
No ratings yet
Unit 4
22 pages
OOP'S - Module - II - (Polymorphism) Notes
PDF
No ratings yet
OOP'S - Module - II - (Polymorphism) Notes
16 pages
OOP Revision Notes (SPPU 2019 - CO210243)
PDF
No ratings yet
OOP Revision Notes (SPPU 2019 - CO210243)
8 pages
Lecture 9
PDF
No ratings yet
Lecture 9
14 pages
Oop Pt-2 Question Bank Chapter 4: Pointer and Polymorphism
PDF
No ratings yet
Oop Pt-2 Question Bank Chapter 4: Pointer and Polymorphism
14 pages
OOPS Notes Unit-6
PDF
No ratings yet
OOPS Notes Unit-6
15 pages
Polymorph Is M
PDF
No ratings yet
Polymorph Is M
41 pages
Polymorphism in C
PDF
No ratings yet
Polymorphism in C
8 pages
C++ Polymorphism
PDF
No ratings yet
C++ Polymorphism
6 pages
C++ Polymorphism: Real Life Example of Polymorphism
PDF
No ratings yet
C++ Polymorphism: Real Life Example of Polymorphism
16 pages
Polymorphism in C++ and Types of Polymorphism in C
PDF
No ratings yet
Polymorphism in C++ and Types of Polymorphism in C
10 pages
Batch) .: Name: Aditya Bhalchandra Patil. Branch: Computer Technology. Class: II CM (2 Roll No: 186136
PDF
No ratings yet
Batch) .: Name: Aditya Bhalchandra Patil. Branch: Computer Technology. Class: II CM (2 Roll No: 186136
12 pages
C++ Chapter-5,6
PDF
No ratings yet
C++ Chapter-5,6
13 pages
OOP IMP Que With Ans
PDF
No ratings yet
OOP IMP Que With Ans
9 pages
Ply Morphis M
PDF
No ratings yet
Ply Morphis M
11 pages
Oops 3rd-1
PDF
No ratings yet
Oops 3rd-1
9 pages
Model Test Paper 4
PDF
No ratings yet
Model Test Paper 4
9 pages
OOP Micro
PDF
No ratings yet
OOP Micro
8 pages
Polymorphism: Real Life Example of Polymorphism
PDF
No ratings yet
Polymorphism: Real Life Example of Polymorphism
5 pages
Polymorphism Means More Than One Function With Same Name
PDF
No ratings yet
Polymorphism Means More Than One Function With Same Name
8 pages
Polymorphism
PDF
No ratings yet
Polymorphism
19 pages
Q-1: Explain The Concept of Constructers and Destructors in C+ + With Programming Examples
PDF
No ratings yet
Q-1: Explain The Concept of Constructers and Destructors in C+ + With Programming Examples
21 pages
What Is Function Overloading? Write A C++ Program To Implement A Function Overloading
PDF
No ratings yet
What Is Function Overloading? Write A C++ Program To Implement A Function Overloading
8 pages
Module 3 Polymorphism
PDF
No ratings yet
Module 3 Polymorphism
14 pages