Ex 1: Specify a class member function as a friend of another class?
#include <iostream> #include <string> using namespace std; class Manager; class Employee; class Admin{ public: void EmployeeDetails(Employee &obj); void ManagerDetails(Manager &obj); }; class Employee{ string name = "praful"; friend void Admin::EmployeeDetails(Employee &obj); }; class Manager{ string name = "Robert"; friend void Admin::ManagerDetails(Manager &obj); }; void Admin::EmployeeDetails(Employee &obj) { cout << "Employee :" << obj.name << endl; } void Admin::ManagerDetails(Manager &obj) { cout << "Manager :" << obj.name << endl; } int main() { Employee e; Manager m; Admin a; a.EmployeeDetails(e); a.ManagerDetails(m); return 0; }
Output
Employee :praful
Manager :Robert
Ex 2: const data pointer, const address pointer, const address data pointer ?
#include <iostream> int main() { int x; int y; int z; const int* ConstdataPtr = &x; // data-constant int* const CosntAddPtr = &y; // address-constant const int* const ConstDataAddPtr = &z; // data-address-constant ConstdataPtr = &y; // *ConstdataPtr = 10; // error: assignment of read-only location ‘* ConstdataPtr’ // CosntAddPtr = &x; // error: assignment of read-only variable ‘CosntAddPtr’ *CosntAddPtr = 109; // ConstDataAddPtr = &x; //error: assignment of read-only variable ‘ConstDataAddPtr’ // *ConstDataAddPtr = 100; // error: assignment of read-only location ‘*(const int*)ConstDataAddPtr’ return 0; }
Ex 3: How we use volatile in C++? const_cast Example ?
#include <iostream>
using namespace std;
int main()
{
volatile const int x = 10;
int* ptr = const_cast<int*>(&x);
*ptr = 100;
cout << "x = "<< x << endl;
cout << "*ptr = " << *ptr << endl;
return 0;
}
#include <iostream> using namespace std; int main() { volatile const int x = 10; int* ptr = const_cast<int*>(&x); *ptr = 100; cout << "x = "<< x << endl; cout << "*ptr = " << *ptr << endl; return 0; }
Output: with volatile keyword
x = 100 *ptr = 100
Output: without volatile keyword
x = 10
*ptr = 100
Ex 4: why use static_cast <> in c++ ?
#include <iostream> class Base{}; class Derived: private Base{}; int main() { char ch = 'A'; // 1 byte char *chPtr = &ch; // 1 byte Ptr // int *intPtr = (int*)(chPtr); // no error int *intPtr = static_cast<int*>(chPtr); //error: invalid ‘static_cast’ from type ‘char*’ to type ‘int*’ Derived d1; // Base *bPtr = (Base*)(&d1); // no error Base *bPtr = static_cast<Base*>(&d1); // error: ‘Base’ is an inaccessible base of ‘Derived’ return 0; }
EX 5: Why use dynamic_cast<> in c++?
#include <iostream> using namespace std; class Base { virtual void fun(){ } // must have one virtual function to use dynamic_cast<> }; class Derived_1:public Base{}; class Derived_2:public Base{}; int main() { Derived_1 d1; Base *bptr = dynamic_cast<Base*>(&d1); Derived_2 *dptr = dynamic_cast<Derived_2*>(bptr); if(dptr==NULL) cout << "Issue with type_casting" << endl; return 0; }
Output:
Issue with type_casting
EX 6 : How and Why use reinterpret_cast?
#include <iostream> #include <string.h> using namespace std; struct Vendor_Data{ int id; char data[16]; bool flag_bit; }; Vendor_Data* vendor_api() { Vendor_Data *d1 = new Vendor_Data{1,"Praful Vanker",0}; return d1; } int main() { void *data = reinterpret_cast<void*> (vendor_api()); int *ptr = static_cast<int*>(data); cout << *ptr++ << endl; cout << reinterpret_cast<char*>(ptr) << endl; ptr = ptr + strlen(reinterpret_cast<char*>(ptr)); cout << *ptr << endl; delete reinterpret_cast<Vendor_Data*>(data); return 0; }
Output:
1
Praful Vanker
0
EX 7: convert string to integer ? stringstream
#include<iostream> #include<sstream> using namespace std; int my_atoi(const char *str) { int data=0; int index = 0; int sign = 1; if(str[index]=='-') { sign = -1; index++; } for(;str[index];++index) { data = data*10 + str[index] - '0'; } return data*sign; } int my_atoi_stringstream(const char *str) { stringstream ss; ss << str; int x; ss >> x; return x; } int main() { cout << my_atoi(static_cast<const char*>("-123")) << endl; cout << my_atoi_stringstream(static_cast<const char*>("-321")) << endl; return 0; }
Output:
-123
-321
EX 8 : Find number of 1's in number ? (Kernighan's way)
#include<iostream> int main() { int number = 7; // 0000 0111 int num_of_ones; //Kernighan's way for(num_of_ones=0;number; ++num_of_ones) number &= number - 1; std::cout << "number of 1's= " << num_of_ones << std::endl; return 0; }
Output:
3
EX 9 : Fibonacci, palindrome, prime no example ?
#include <iostream> using namespace std; int fiboWithoutRec(int n){ int a = 0, b = 1, c, i; for (i = 0; i < n; i++){ c = a + b; a = b; b = c; } return c; } int fiboWithRec(int n){ if (n <= 1) return 1; else return fiboWithRec(n - 1) + fiboWithRec(n - 2); } bool is_prime(int n){ for (int i = 2; i < n / 2; i++){ if (n % i == 0) return false; } return true; } void print_prime_sr(int start, int end){ for (int i = start; i <= end; i++){ if (is_prime(i)) cout << i << " "; } cout << endl; } bool is_palindrom(int n){ int sum = 0; int temp = n; for (; n; n = n / 10) sum = (sum * 10) + n % 10; if (sum == temp) return true; else return false; } int main() { cout << fiboWithRec(5) << endl; cout << fiboWithoutRec(5) << endl; cout << is_prime(9) << endl; print_prime_sr(100, 200); cout << is_palindrom(121); return 0; }
Output:
8
8
0
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
8
8
0
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
Ex 10: Explain virtual function with example ?
#include <iostream> using namespace std; class Base{ public: virtual void func(){cout << "Base func()" << endl;} }; class Derived1:public Base{ void func(){cout << "Derived1 func()" << endl;} }; class Derived2:public Base{ void func(){cout << "Derived2 func()" << endl;} }; void fun(Base &obj){ cout << "func(Base &)" << endl; obj.func(); } void fun(Base *obj){ cout << "func(Base *)" << endl; obj->func(); } int main(){ Derived1 d1; Derived2 d2; cout << " ------------------ Pointer ------------------" << endl; Base *bptr = &d1; fun(bptr); // func(Base *) -> Derived1 func() bptr = &d2; fun(bptr); // func(Base *) -> Derived2 func() cout << " ------------------ Reference ------------------" << endl; Base &bref1 = d1; fun(bref1); // func(Base &) -> Derived1 func() Base &bref2 = d2; fun(bref2); // func(Base &) -> Derived2 func() return 0; }