NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


2) virtual function ?

- its is declared in base class with keyword virtual and redefined in derived class.
-> virtual function are dynamic.
-> they are always declared in base class and overridden in child class.
-> they called during runtime.
-> virtual function cannot be static.
->Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism.

-> virtual is a keyword in c++.
-> a virtual function is redefined in derived class.
-> when a virtual function is defined in base class, then the pointer to base class is created . now , on the basis of type of object assigned to respective class function is called .
-> its is also called virtual base class. and its also are late binding .

example

#include<iostream.h>
class A {
public:
virtual void show (){
court<<"n base class";
}

};

class B : public A {
public:
void show (){
court<<"n derived class";
}
};

int main (){
A* bptr ;
B aa;
bptr = & aa;
bptr => show();
return 0;
}

* explanation

1)Without Virtual Function( when you don't write virtual keyword that why call base class)

-> When the show function is not virtual, the function call basePtr->show() is resolved at compile time. The compiler decides to call the show function of the Base class because basePtr is of type Base*

2) With Virtual Function:( when write virtual keyword in base )
When the show function is declared as virtual in the Base class, the function call basePtr->show() is resolved at runtime (dynamic binding). The compiler decides to call the show function of the Derived class because basePtr points to an object of the Derived class.


Q2) friend function ?

-> we know that the private section of class is accessible only & only through the public section of the same class.
what if we want to give access to private member, a function outside the class , in such circumstances we use the concept of friend function.

-> A friend function in C++ is a function that is not a member of a class but has access to the class's private and protected members. It is declared within the class using the keyword friend. This allows the friend function to access the private and protected data of the class.

key points
-> a friend function cant be called using the object of the class.so it is called like a normal function.
-> friend function can use the resources of a class only using an object of same class.
-> usually a friend function has object as an argument.
-> it doest not any affect its is private,public section.

example

#include<iostream>
using namespace std;

class demo{
int a,b;
public:
void getdata();
friend int sum (demo);
};

void demo::getdata(){
cout<<"n enter two number";
in>>a>>b;
}

int sum(demo aa){
return (aa.a + aa.b);
};

int main (){
demo aa;
aa.getdata();
cout<<"addition ="<<sum(aa);
return;
};


q3) static data member?
-> static member divide into two parts static data member and static member function

-> it is initialized to zero , whenever the first object of its class is created . no other initialization is permitted.

-> for making any data member static we use static keyword.
-> only one copy of static data member is created & share by all.
-> its is visibility entire program and use .

example

#include<iostream.h>
using namespace std;

class demo {
int x,y;
static int y;
public:
void getdata( int a, int b){
x = a;
y=b;
z= z+1;
}
void putdata(){
cout<<"x =" <<x <<"y =" <<y <<"z =" <<z;
}
};

int demo::z;

int main(){
demo aa,bb;
aa.getdata(10,5);
bb.getdata(20,30);
aa.putdata();
bb.putdata();
return 0;
}

output
x = 10 y= 5 z= 2;
x= 20 y= 30 z= 2;


q3) static member function ?

example

-> class ABC {
int a,b <-- data member ;
static int z ; < -- static data member
public:
void getdata();
void putdata(); <-- member function
static void myfunc(); <-- data member function


->static member function can access only static data member .
-> its not a part of object because it is independent member.so it is call using the class name.
->Static member functions can be called using the class name without creating any objects.
->Static member functions can only access static data members or other static member functions.

example code

#include<iostream.h>

class demo {
int x;
static int y;
public:
void getdata( int a){
x= a;
y=y+1;
}
void putdata(){
cout<<"n x =" <<x <<"n y=" <<y;
}
static void abc(){
cout<<"n y=" <<y;
}
};
int demo :: y;
int main(){
demo aa;
aa.getdata(5);
aa.putdata();
demo::abc();
};


q4)ambiguity resolution in c++

#include<iostream.h>
using namespace std;

class A {
protected:
int a;
public :
void input(){
cout<<"n enter value";
cin>> a;
}
void output(){
cout<<"n a="<<a;
}
};

class B {
protected:
int b;
public :
void input(){
cout<<"n enter value";
cin>> b;
}
void putdata(){
cout<<"n b="<<b;
}
};

class C {
int c;
public :
void input(){
cout<<"n enter value";
cin>> c;
A::input();
B::input();
}
void display(){
cout<<"n C="<<c;
}
};

int main(){
c aa;
aa.input();
aa.display();
aa.output();
aa.putdata()
}


q5) template ? function template and class template?

-> A template in C++ is a powerful feature that allows you to write generic and reusable code. Instead of writing the same code for different data types, you can use templates to define a single code structure that works with any data type

Types of Templates

C++ supports two main types of templates:

-Function Templates
- Class Templates


-> function template

-> Function templates allow you to create a function that can operate with any data type. This is useful when you want the same function to work with different types of data.


#include<iostream.h>
using namespace std;

template<class T>
void show(T a, T b){
cout<<"A="<<a <<"B=" <<b ;
}

int main(){
int p = 10 , q= 20;
char m= 'a' n='b';
float s =10.2 , f = 13.6;
show(p,q);
show(m,n);
show(s,f);
}

* class template

-> Class templates allow you to create a class that can operate with any data type. This is useful when you want a class to be able to store or manage different types of data.


#include<iostream.h>
using namespace std;

template<class T>
class show {
T a, T b;
public:
show(T p, T q){
a= p ;
b= q;
}

void show (){
cout<<"A="<<a <<"B="<<b;
}
};

void main(){
show<int> ob(10,20);
ob.show();
};

















     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.