---------------------- ---------------------------- C ++ module -------------------------------------------
Narayan Lal Menariya
------------------------------------------------------------------------------------------------------------------
february 20,2018 --- 1st day
1.sample1.cpp
2.namespace.cpp
3.defaultargument.cpp
4.inline.cpp
5.references.cpp
6.swapusingREFERENCES.cpp
7.references1.cpp
8.functionoverloading.cpp
9.functionoverloading1.cpp
10.structure.cpp
11. structure.c // its c program to verify difference between structure in c and c++
--------------------------------------------- february 21,2018 ----- 2nd day -------------------------------------------
1.private.cpp
2.class_function.cpp
3.class_function1.cpp
4.class_student.cpp
5.constructor.cpp
6.param_constructor.cpp
7.student_constructor.cpp
8.Defaultcompliler_constructor.cpp
9.destructor.cpp
10.parameterized_const&destructor.cpp
--------------------------------------------- february 22, 2018 ----------------------------------------------------
Dynamic memory allocation:
1. NewOperator.cpp
2. newfordynamicARRAY.cpp
3. memoryForObject.cpp
4. malloc.cpp
5. parameterized_constUsingDMA.cpp
6. default_paraConstUsingDMA.cpp
7. ThisPointer.cpp
8. writingconstructor&display_OUTSIDEclass.cpp
9. Deepcopy.cpp
10. shallowCopy.cpp
------------------------------------------------- february23, 2018 -----------------------------------------------------
1.const_datamember.cpp
2.const_memberfunction.cpp
3.const_object.cpp
4.static.cpp
5.static_memberfunction.cpp
6.static_localVariable.cpp
7.newfor2DArray.cpp
-------------------------------------- february26, 2018 -------------------------------------------------------
1.friend.cpp
2.friend_second.cpp
3.friend_passbyvalue.cpp
4.friend_passbyReference.cpp
5.friend_InClass.cpp
6.friend_calculator.cpp
7.Opeartor_Overloading_Pre_increment.cpp
8.Opeartor_Overloading_for_friend.cpp
9.operator_overloading_Post_increment.cpp
10.operator_overloading_Post_increment_friend.cpp
11.Binary_Operator_overloading.cpp
12.Return_Binary_Operator_overloading.cpp
13.Binary_Operator_overloading_friend.cpp
----------------------------------- jbu -------------------------------------
------------------------------- february 27,2018 ----------------------------
1.Bitwisw_NotOperator_overloading.cpp
2.Bitwise_OR_operator_overloading.cpp
3.Real&IMG_overloading.cpp
4.string.cpp
5.String_function.cpp
6.String_comparison.cpp
7.My_string_Overload.cpp : assignment question to perform overloading on string function
8.read_space_In_string.cpp
-------------------------------------- february 28,2018 -------------------------
1.Inheritance.cpp
2.Inheritance_demo.cpp
3.Inheriting_dataMemberFrom_baseTOderivedClass.cpp
4.Inheritance_Initializing_parameterized_constructorofBASEclass.cpp
5.Inheritance_Initializing_parameterized_constructorofBASEclass_1.cpp
6.Inheritance_Copy_constructor.cpp
7.Inheritance_fruit.cpp
------------------------------- March1, 2018 -its the occassion of holy -----------------------
1.Multilevel_Inheritance.cpp
2.Hierarchical_inheritance.cpp
3.Multiple_inheritance.cpp
1. Sample1.cpp
#include<iostream>
int main()
{
//std::cout<<"Hello world:n";
std::cout<<"Hello world:n"<<std::endl;;
int a;
std::cin>>a; // >> extraction operator ,,,,, << insertion operator
std::cout<<"a :"<<a<<std::endl;
// cin and cout are called objects of iostream while printf() and scanf() are functions
// :: called scope of resolution operator
}
2. Namespaces.cpp
// namespaces are used to avoid global collision for same variable
#include<iostream>
//int a =10;
//int a=23; // its is not allowed to use same variable for two different values
/*
namespace cranes
{
int a = 10;
}
namespace varsity
{
int a=23;
}
int main()
{
std::cout<<"cranes a:"<<cranes::a<<std::endl;
std::cout<<"varsity a:"<<varsity::a<<std::endl;
}
*/
//-------------------------------------------------------------------
/*
namespace cranes
{
int a = 10;
}
namespace varsity
{
int a=23;
}
using namespace varsity; // it is used to print bydefault value of second time declared variable
int main()
{
std::cout<<"a :"<<a<<std::endl;
}
//---------------------------------
*/
using namespace std; // we are creating std as default
namespace cranes
{
int a = 20;
}
namespace varsity
{
int a=25;
}
using namespace varsity; // it is used to print bydefault value of second time declared variable
int main()
{
cout<<"a :"<<a<<endl;
}
3. Defaultargument.cpp
#include<iostream>
using namespace std;
int fun(int a, int b,int c=0, int d=0);
//int fun(int a, int b,int c=0, int d); // error because assigning is from right to left
int main()
{
cout<<fun(1,2)<<endl; // a=1,b=2
cout<<fun(1,2,3)<<endl; // a=1,b=2,c=3
cout<<fun(1,2,3,4)<<endl; // a=1,b=2,c=3,d=4
}
int fun(int a, int b, int c, int d)
{
return a+b+c+d;
}
4. Inline.cpp
#include<iostream>
using namespace std;
#define swp(a,b) a=a+b; b = a-b; a=a-b; cout<<"a:"<<a<<endl;
// macros are consuming more memory but they are faster than function
/*
void swap(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
cout<<"a= "<<a<<" b = "<<b<<endl;
}
*/
// inline will make function swap(10,20)to work like macro function swp(a,b)
inline void swap(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
cout<<"a= "<<a<<" b = "<<b<<endl;
}
int main()
{
int a=30,b=20;
swp(a,b);
swap(10,20);
}
5. References.cpp
// references are like pointer but there is no need of dereferencing
// always refences should be initialized at the time of decleration
// type &reference_name=variable_name; // no need of writing & before variable_name
#include<iostream>
using namespace std;
int main()
{
int a=12;
//int &ref =12 // can not point to a constant value
// int &ref; // is also not allowed
// int &ref=NULL; // is also not allowed because NULL = 0 which is constant value
int &ref = a; // no need of writing like ref = &a
cout<<"ref : "<<ref<<endl;
cout<<"a's address :"<<&a<<endl;
cout<<"ref's address : "<<&ref<<endl;
// &a and &ref are giving same value it means memory is not allocated for ref variable
}
6. swapusingREFERENCES.cpp
#include<iostream>
using namespace std;
//void swap(int a, int b)
void swap(int &a, int &b) // here we are declaring and initialing a and b
{
a=a+b;
b=a-b;
a=a-b;
cout<<"a= "<<a<<" b = "<<b<<endl;
}
int main()
{
int a=10,b=20;
int &refA=a, &refB = b;
swap(refA,refB);
}
7. references1.cpp
#include<iostream>
using namespace std;
int main()
{
int a=10;
int &ref = a;
ref++;
cout<<"a ="<<a<<endl;
a++;
cout<<"a ="<<a<<endl;
cout<<"sizeof(int&) :"<<sizeof(int&)<<endl;
cout<<"sizeof(char& :)"<<sizeof(char&)<<endl;
// size of reference variable varies according to datatype of variable but in pointer it is always
fixed
}
8. functionoverloading.cpp
/* function overloading : ---features of c++ that allows multiple function to have
same function name ....
multiple function may have same function name either in differ with type of argument
or order of argument or number of argument
*/
#include<iostream>
using namespace std;
void fun(int a, int b)
{
cout<<"a :"<<a <<" ,b :"<<b<<endl;
}
void fun(int a, int b,int c)
{
cout<<"a :"<<a<< " ,b :"<<b<<"c :"<<c<<endl;
}
//void fun(float a, int b)
// will give error as c++ by default taking float value as double
void fun(double a, int b)
{
cout<<"a :"<<a <<" ,b :"<<b<<endl;
}
void fun(int a, double b)
{
cout<<"a :"<<a <<" ,b :"<<b<<endl;
}
void fun(int a, float b)
{
cout<<"a :"<<a <<" ,b :"<<b<<endl;
}
int main()
{
fun(1,2);
fun(2.3,57);
fun(57,4.5);
fun(34,3.4f);
// writing f will make 3.4 float ---- if not writing f it will be considered as double
fun(1,2,3);
}
9. functionoverloading1.cpp
#include<iostream>
using namespace std;
#define PI 3.14
void area(int);
void area(float);
void area(int,int);
int main()
{
int side=10;
float rad=12.2f;
int length = 15,breadth=4;
area(side);
area(rad);
area(length,breadth);
}
void area(int side_sqr)
{
cout<<"Area of square with side :"<<side_sqr<<" is :"<<"t";
cout<<side_sqr*side_sqr<<endl;
}
void area(float radius)
{
cout<<"area of circle with radius = "<<radius<<" is"<<"t";
cout<<PI*radius*radius<<endl;
}
void area(int len,int bre)
{
cout<<"Area of rectangle with len="<<len<<" and";
cout<<" breadth = "<<bre<<" is :"<<"t";
cout<<len*bre<<endl;
}
10. structure.cpp
/*
difference between structure in c and c++ ------------- page 19
1. structure in c++ contain both data and function variable while in c structure
contains only data member
2. need not use the struct keyword while creating the variable but in c needed
3. size of empty structure is 1 byte in gcc compiler but in c
size is 0
4. can have static data member but in c cannot have static data member
5. access specifier can be used for providing data security but in c no concept
of access specifier
difference between private and public
*/
#include<iostream>
using namespace std;
int fun(int a, int b);
struct student
{
private:
int rollnum;
char name[10];
public:
int fun(int a, int b)
{
return a+b;
}
};
struct emptystructure
{
};
int main()
{
char name[10];
int rollnum;
//struct student s;
student s;
emptystructure emp;
cout<<"Enter name and roll number :"<<endl;
cin>>name>>rollnum;
int c = s.fun(3,4);
cout<<"c :"<<c<<endl;
cout<<"sizeof student :"<<sizeof(s)<<endl;
cout<<"sizeof emptystructure :"<<sizeof(emp)<<endl;
return 0;
}
/*
----------------- structure.c ---------------------
#include<stdio.h>
//using namespace std;
struct student
{
//private:
int rollnum;
char name[10];
// public:
//int fun(int a, int b)
//{
// return a+b;
//}
};
struct emptystructure
{
};
int main()
{
char name[10];
int rollnum;
//student s; // error
struct student s;
struct emptystructure emp;
printf("%u",sizeof(emp));
//printf("Enter name and roll number :");
//scanf(%s,%d,&s.name,&s.rollnum);
//int c = s.fun(3,4);
//printf("c :%d",c);
//cout<<"sizeof student :"<<sizeof(s)<<endl;
//cout<<"sizeof emptystructure :"<<sizeof(emp)<<endl;
return 0;
}
*/
11. structure.c // its c program to verify difference between structure in c and c++
#include<stdio.h>
//using namespace std;
struct student
{
//private:
int rollnum;
char name[10];
// public:
//int fun(int a, int b)
//{
// return a+b;
//}
};
struct emptystructure
{
};
int main()
{
char name[10];
int rollnum;
//student s;
struct student s;
struct emptystructure emp;
printf("%u",sizeof(emp));
//printf("Enter name and roll number :");
//scanf(%s,%d,&s.name,&s.rollnum);
//int c = s.fun(3,4);
//printf("c :%d",c);
//cout<<"sizeof student :"<<sizeof(s)<<endl;
//cout<<"sizeof emptystructure :"<<sizeof(emp)<<endl;
return 0;
}
---------------------------------------- february 21,2018 ----- 2nd day ----------------------------------------------------
1. private.cpp
/*
C++ features
1. Abstraction
2. Encapsulation
3. polymorphism
4. Inheritance
*/
#include<iostream>
using namespace std;
//class test
//{
//public:
// int a;
// char c;
//
//};
//int main()
//{
// //objects are instance of class
// // here o1 is object
//
// test o1={10,'c'};
//
// cout<<o1.a<<endl<<o1.c<<endl;
//
//
//}
// ---------------- private------------
class test
{
private:
int a;
char c;
};
int main()
{
//objects are instance of class
// here o1 is object
test o1={10,'c'};
cout<<o1.a<<endl<<o1.c<<endl;
}
// private will give error
//error: c and a is private
2. class_function.cpp
/*
private: data member under this access specifier can be accessed only with
in the class
public: can be ACCESSED outside the class
*/
#include<iostream>
using namespace std;
class test
{
// test is tag name
//private:
// int a;
// char c;
// void display()
// {
// cout<<"inside class"<<endl;
// }
/*
will give error: display is private
*/
private:
int a;
char c;
public:
// void display()
// {
// cout<<"inside class"<<endl;
// cout<<"a= "<<a<<" c = "<<c<<endl;
// }
void init(int x, char ch)
{
a=x;
c=ch;
}
void display()
{
cout<<"inside class"<<endl;
cout<<"a= "<<a<<" c = "<<c<<endl;
}
// display function can be written below or above the init() function
};
int main()
{
//objects are instance of class
// here o1 is object
test o1;
o1.init(10,'A');
o1.display();
/* o1.init(10,'A'); // error: will print garbage value because we are displaying
before initialing/ assigning value in a and c
*/
}
3. class_function1.cpp
/*
private: data member under this access specifier can be accessed only with in the class
public: can be ACCESSED outside the class
*/
#include<iostream>
using namespace std;
class test
{
//private:
// int a;
// char c;
int a;
char c; // bydefault they are private access specifier only
public:
void init(int x, char ch)
{
a=x;
c=ch;
}
void display()
{
cout<<"inside class"<<endl;
cout<<"a= "<<a<<" c = "<<c<<endl;
}
};
int main()
{
test o1,o2;
o1.init(10,'A');
o1.display();
o2.init(20,'B');
o2.display();
/* Each object(o1 and o2 both ) and each variable(a and c both ) will get
different memory
*/
}
/*
structure : public is by default access specifier
class: private is by default access specifier
*/
4. class_student.cpp
/*
Problem: create a class
make all data member private
input and display
*/
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
char name[20];
int id;
float marks;
public:
void init(const char *nam, int idn, float mark)
{
strcpy(name,nam); // strcpy(destination,source);
id = idn;
marks= mark;
}
void display()
{
cout<<"name :"<<name<< " id :"<<id<<" marks :"<<marks<<endl;
}
};
int main()
{
student s;
char Name[20];
int Id;
float Marks;
cout<<"Enter student name, id and marks"<<endl;
cin>>Name>>Id>>Marks;
s.init(Name,Id,Marks);
s.display();
return 0;
}
5. constructor.cpp
/*
constructor:
constructor name should having only what is class name
a. default constructor:
1.constructor which is not having any argument
2. there is no need of calling default constructor
3. automatically compiler will call the default constructor
*/
#include<iostream>
using namespace std;
class test
{
int a;
char name[20];
public:
// ------------------- default constructor ----------------
test()
{
cout<<"Default constructor "<<endl;
}
void display()
{
cout<<"In display "<<endl;
}
};
int main()
{
test obj;
obj.display();
test obj1; // default constructor will get called
}
6. param_constructor.cpp
/*
b. parameterized constructor : constructor which is having arguments or parameters
c. copy constructor : when creating a new object from existing object
*/
#include<iostream>
#include<string.h>
using namespace std;
class test
{
int a;
char name[20];
public:
// ------------------- default constructor ----------------
test()
{
cout<<"--------- In Default constructor -----------"<<endl;
}
// --------------------- parameterized constructor ---------
test(int x, const char*s)
{
cout<<"-------- In parameterized constructor -----------"<<endl;
a=x;
strcpy(name,s);
}
// ------------------ copy constructor -------------------
// test (test m) // its recursive call thus need to pass reference as shown below
test(test &m)
{
cout<<"-------- In copy constructor -----------"<<endl;
a = m.a;
strcpy(name, m.name);
}
void display()
{
cout<<"-------- In display function --------------"<<endl;
cout<<"a :"<<a<<" name :"<<name<<endl;
}
};
int main()
{
test obj; // default constructor will get called
//test obj(12,"varsity"); // error: redeclaration of obj
test obj1(12,"varsity"); // it will call to parameterized constructor
obj1.display();
cout<<"------ In main function -----------"<<endl;
// test obj2=obj1;
test obj2(obj1); // it will call to copy constructor
obj2.display();
}
7. student_constructor.cpp
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
char name[20];
int id;
float marks;
public:
student(const char *nam, int idn, float mark) // parameterized constructor
{
cout<<"--------- In Parameterized constructor -----------"<<endl;
strcpy(name,nam); // strcpy(destination,source);
id = idn;
marks= mark;
}
student(student &m) // copy constructor
{
cout<<"--------- In copy constructor -----------"<<endl;
strcpy(name,m.name); // strcpy(destination,source);
id = m.id;
marks= m.marks;
}
void display()
{
cout<<"name :"<<name<< " id :"<<id<<" marks :"<<marks<<endl;
}
};
int main()
{
// student s; // will give error because there is no default constructor
char Name[20];
int Id;
float Marks;
cout<<"Enter student name, id and marks"<<endl;
cin>>Name>>Id>>Marks;
student s(Name,Id,Marks); // using parameterized constructor
s.display();
student s1(s); // using copy constructor
s1.display();
return 0;
}
8. Defaultcompliler_constructor.cpp
/*
b. parameterized constructor : constructor which is having arguments or parameters
c. copy constructor : when creating a new object from existing object
*/
#include<iostream>
#include<string.h>
using namespace std;
class test
{
int a;
char name[20];
public:
void display()
{
cout<<"-------- In display function --------------"<<endl;
cout<<"a :"<<a<<" name :"<<name<<endl;
}
};
int main()
{
test obj(12,"varsity"); // it will call to parameterized constructor
obj.display();
test obj2(obj); // it will call to copy constructor
obj2.display();
}
9. destructor.cpp
/*
Destructor: to release the memory of objects destructor will be used
when object is going out of scope --- automatically destructor will get call
*/
#include<iostream>
#include<string.h>
using namespace std;
class test
{
public:
test() // constructor
{
cout<<"Default constructor:n";
}
~test() // destructor
{
cout<<"Destructor:n";
}
};
void fun()
{
cout<<"In fun:n";
test o1;
cout<<"end of fun:n";
}
int main()
{
cout<<"In main calling to fun:n";
fun();
cout<<"end of main:n";
}
10. parameterized_const&destructor.cpp
/*
Destructor: to release the memory of objects destructor will be used
when object is going out of scope --- automatically destructor will get call
*/
#include<iostream>
#include<string.h>
using namespace std;
class test
{
int a;
char c;
public:
test() // constructor
{
cout<<"Default constructor:n";
}
~test() // destructor
{
cout<<"in Destructor:n";
}
test (int x,char ch)
{
cout<<"In parameterized constructor:n";
x=a;
c=ch;
cout<<"end of param. const:n";
}
void display()
{
cout<<"----- In display:nn";
// cout<<"a = "<<a<<"c ="<<c<<endl;
// cout<<"end of displaynn";
}
};
void fun()
{
{
cout<<"In fun:nn";
test o1; // o1 is getting memory in stack segmentation only
cout<<"end of fun:nn";
}
cout<<"calling to parameterized contructor:nn";
test obj(1,'A');
obj.display();
}
int main()
{
cout<<"In main calling to fun:nn";
fun();
// cout<<"calling to parameterized contructor:n";
// test obj(1,'A');
// obj.display();
cout<<"end of main:nn";
// first disable test.obj() from fun and observe ..... than disable from
//main function and observe when written in fun()
}
/----------------------------------- ------------ february 22, 2018 ----------------------------------------
Dynamic memory allocation:
1. NewOperator.cpp
/*
Dynamic memory allocation : two operator
1. New : for allocating the memory
2. Delete : for deallocating the memory
Pointer = new data type
return type of new and delete is *pointer
*/
#include<iostream>
using namespace std;
int main()
{
int *p = new int(100);
int *pointr = new int;
*pointr = 100;
cout<<"*pointer :"<<*pointr<<endl;
cout<<"Size of int pointer :"<<sizeof(pointr)<<"n";
cout<<"Size of int pointer p = int(100) :"<<sizeof(p)<<"n";
char *ch = new char;
*ch = 'A';
cout<<"Size of char pointer :"<<sizeof(ch)<<"n";
delete pointr;
delete ch;
cout<<"value on *pointer after deletion of pointer :"<<*pointr<<endl;
cout<<"after deleting Size of char pointer :"<<sizeof(ch)<<"n";
}
2. newfordynamicARRAY.cpp
/*
dynamic memory allocation for run time array
we are allocating memory in heap segment only
*/
#include<iostream>
using namespace std;
int main()
{
int *p = new int[5];
/*
cout<<"Size of empty pointer :"<<sizeof(p)<<"n";
for(int i=0;i<5;i++)
{
cout<<"*p :"<<*(p+i)<<endl;
}
*/
cout<<"Enter value :"<<endl;
// for(int i=0;i<5;i++)
// {
// cin>>*(p+i);
//
// }
for(int i=0;i<5;i++)
{
cin>>p[i];
}
// for(int i=0;i<5;i++)
// {
// cout<<"*p :"<<*(p+i)<<endl;
// }
for(int i=0;i<5;i++)
{
cout<<"*p :"<<*p<<endl;
p++;
}
cout<<"Size of pointer :"<<sizeof(p)<<"n";
// to delete entire array
delete [] p;
}
3. memoryForObject.cpp
/*
Memory for object
*/
#include<iostream>
using namespace std;
class test
{
public:
test()
{
cout<<"Default constructor :n";
}
~test()
{
cout<<"Destructor :n";
}
};
int main()
{
/*
test *obj = new test;
delete obj;
*/
/*
Here obj is pointer type object for class
this obj is getting memory in heap segment
but in parameterized_const&destructor.cpp o1 is getting memory in stack segment
*/
test *obj = new test[5];
/* here creating 5 time object thus five time it will call to default constructor
and destructor
*/
delete [] obj;
}
4. malloc.cpp
/*
difference b/w malloc and new
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class test
{
public:
test()
{
cout<<"Default constructor :n";
}
~test()
{
cout<<"Destructor :n";
}
};
int main()
{
test *ptr = (test*)malloc(sizeof(test));
free(ptr);
/*
default constructor will not get called
*/
}
5. parameterized_constUsingDMA.cpp
/*
because ptr is pointer type object thus instead of . operator use -> operator
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class test
{
int a;
public:
test(int x)
{
a=x;
cout<<"Parameterized constructor :n";
}
void display()
{
cout<<"a :"<<a<<endl;
}
~test()
{
cout<<"In Destructor :n";
}
};
int main()
{
test *ptr = new test(10);
ptr ->display();
delete ptr;
}
6. default_paraConstUsingDMA.cpp
/*
because ptr is pointer type object thus instead of . operator use -> operator
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class test
{
int a;
public:
test(int x=0)
{
a=x;
cout<<"Parameterized as well as default constructor :n";
}
void display()
{
cout<<"a :"<<a<<endl;
}
~test()
{
cout<<"In Destructor :n";
}
};
int main()
{
test o1; // this will call to default constructor --- test(int x=0)
cout<<"IN main :n";
test *ptr = new test(10);
// this will call to parameterized constructor---test(int x=0)
ptr ->display();
delete ptr;
}
7. shallowCopy.cpp
/*
shallow copy:
when having inbuild copy constructor ,, both object(str1 and str2) will share
same memory
thus when we comment that part it shows error ;; core dumped i.e segmentation failed
*/
/*
Deepcopy: when we are using user defined constructor each object(str1 and str2) will
get different memory
*/
#include<iostream>
#include<cstring>
using namespace std;
class String
{
private:
char *s;
int size;
public:
String(const char *str = NULL); // constructor
~String(){delete []s;} // ----- used to release the memory of object
String(const String&); // copy constructor
void print() {cout<<s<<endl;} // function to print string
void change(const char *); // function to change
};
String:: String(const char *str)
{
size = strlen(str);
s = new char[size+1];
strcpy(s,str);
}
void String::change(const char *str)
{
delete [] s;
s= new char[size+1];
strcpy(s,str);
}
/*
String::String(const String& old_str)
{
size=old_str.size;
s = new char[size+1];
strcpy(s,old_str.s);
}
*/
int main()
{
String str1("cranes");
String str2 = str1;
str1.print(); // what is printed
str2.print();
str1.change("banglore");
str2.change("CranesVarsity");
str1.print(); // what is printed
str2.print();
return 0;
}
----------------------------------------------- february23, 2018 -----------------------------------
1. const_datamember.cpp
/*
initializer list is used to initialize the const as well as non-const variable
*/
#include<iostream>
using namespace std;
class test
{
const int a,b;
int c;
public:
// test(int x=0,int y=0) default constructor as well as parameterized constructor
test(int x=0,int y=0):a(x),b(y),c(10)
{
// a=x,b=y; no need to write like this
// initializer list .... non-const variable can be started using any variable
}
void display()
{
cout<<a<<" "<<b<<" "<<c<<endl;
}
};
int main()
{
test o1(10,20);
o1.display();
}
2. const_memberfunction.cpp
/*
const member function can access/use all the data member of a class but can not change
any of the values.
*/
#include<iostream>
using namespace std;
class test
{
const int a,b;
int c;
public:
test(int x=0,int y=0):a(x),b(y),c(10)
{
}
/*
void display()
{
a=50; // in simple member function data member can be modified
cout<<a<<" "<<b<<" "<<c<<endl;
}
*/
void display()const
{
a=50;
/*
but in the const member function data member can not modified they are
read only object
*/
cout<<a<<" "<<b<<" "<<c<<endl;
}
};
int main()
{
test o1(10,20);
o1.display();
}
3. const_object.cpp
/*
const object can call only const member function
but a normal object can call to const member function
*/
#include<iostream>
using namespace std;
class test
{
const int a,b;
int c;
public:
test(int x=0,int y=0):a(x),b(y),c(10)
{
}
// void display()
// {
// cout<<a<<" "<<b<<" "<<c<<endl;
// }
void display()const
{
cout<<a<<" "<<b<<" "<<c<<endl;
}
};
int main()
{
const test o1(10,20);
test o2(30,40);
// here o1 is const object
o1.display();
o2.display();
}
4. static.cpp
/*
writing int a; will get memory in data segment ,,,, can be accessed using &a
................................
1.there is no static keyword in c structure
2.static data member can not be initialized inside the class
they can only be initialized outside the class
to initialize a static data member outside the class follow the syntax:
data_type class_name::data_member=value;
................. here static data member(a) is not related with
object(o1 and o2) ,,, it is only with respect to class .... thus
every object(o1 and 02) will get only same/ single memory .....
in previous case both object were having different memory
3. default value of static variable is 0
*/
#include<iostream>
using namespace std;
class test
{
static int a;
public:
void display()
{
cout<<"a :"<<a<<endl;
}
};
int test::a=10;
int main()
{
test o1;
o1.display();
test o2;
o2.display();
}
5. static_memberfunction.cpp
#include<iostream>
using namespace std;
class test
{
static int a;
int b=20;
public:
static void display()
{
//cout<<"a :"<<a<<" b :"<<b<<endl;
/*
static member function can not access non-static data member of a class
*/
cout<<"a :"<<a<<endl;
}
};
int test::a=10;
int main()
{
test o1;
o1.display();
test::display();
}
6. static_localVariable.cpp
/*
#include<iostream>
using namespace std;
class test
{
int b;
public:
test()
{
cout<<"Default constructor n";
}
~test()
{
cout<<"Destructor n";
}
};
void fun()
{
test o1;
}
int main()
{
fun();
cout<<"IN main n";
}
ans:
default constructor
destructor
In main
*/
#include<iostream>
using namespace std;
class test
{
int b;
public:
test()
{
cout<<"Default constructor n";
}
~test()
{
cout<<"Destructor n";
}
};
void fun()
{
static test o1;
}
int main()
{
fun();
cout<<"In main n";
}
// -----------------------------------------------------------------
/*
scope of local static variable (static test o1) is through out the program,,,,
thus output is
------------------ destructor get called scope is ending
default constructor
In main
destructor
*/
7. newfor2DArray.cpp
#include<iostream>
#define row 3
#define col 3
using namespace std;
int main()
{
int (*p)[col] = new int[row][col]; // pointer to an array: always use column size
//int *p[col] = new int[row][col]; // Array pointer
cout<<"Enter value :"<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>p[i][j];
}
}
cout<<"matrix :n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<"p[i][j] :"<<p[i][j]<<endl;
}
cout<<endl;
}
delete []p;
}
---------------------------------------- February 26,2018 -----------------------------------------------------------------
1. friend.cpp
/*
Friends: to access the private data member outside the class.
friend is a keyword which is used to access private or protected
data member outside the class of non-member function.
*/
#include<iostream>
using namespace std;
class test
{
int a,b;
public:
test(int a=0,int b=0) // default as well as parameterized constructor
{
this ->a=a;
this ->b=b;
}
// void multi(int a, int b);
friend void multi(test);
};
//void test:: multi(int x,int y)
//{
//
//}
void multi(test m)
{
int c;
c = m.a*m.b;
cout<<"c :"<<c<<endl;
}
int main()
{
test o1(10,20);
multi(o1);
}
2. friend_second.cpp
#include<iostream>
using namespace std;
class test
{
int meter;
public:
test(int a=0)
{
meter = a;
}
friend int addfive(test);
};
int addfive(test m)
{
m.meter = 5;
return m.meter;
}
int main()
{
test o1;
cout<<"meter :"<<addfive(o1)<<endl;
}
3. friend_passbyvalue.cpp
#include<iostream>
using namespace std;
class test
{
int meter;
public:
test(int a=0)
{
meter = a;
}
friend int addfive(test);
void display()
{
cout<<"nmeter in class :"<<meter;
}
};
int addfive(test m)
{
m.meter = 5;
return m.meter;
}
int main()
{
test o1;
cout<<"meter in main :"<<addfive(o1)<<endl;
o1.display();
}
4. friend_passbyReference.cpp
#include<iostream>
using namespace std;
class test
{
int meter;
public:
test(int a=0)
{
meter = a;
}
friend int addfive(test&);
void display()
{
cout<<"nmeter in class :"<<meter;
}
};
int addfive(test& m)
{
m.meter = 5;
return m.meter;
}
int main()
{
test o1;
cout<<"meter in main :"<<addfive(o1)<<endl;
o1.display();
}
5. friend_InClass.cpp
/*
class friendship is only one way direction ....
as radius from class A can be accessed in class B
but area from class B can not be accessed in class A
*/
#include<iostream>
using namespace std;
class A
{
int radius;
public:
A()
{
radius = 10;
}
void showA()
{
cout<<"nradius :"<<radius;
}
friend class B;
};
class B
{
int area;
public:
void find_area(A m)
{
area = m.radius*m.radius;
cout<<"narea :"<<area;
}
};
int main()
{
A oA;
B oB;
oA.showA();
oB.find_area(oA);
}
6. friend_calculator.cpp
#include<iostream>
using namespace std;
class cal
{
double a, b;
public:
cal(double a,double b)
{
this ->a=a;
this ->b=b;
}
friend double add(cal);
friend double sub(cal);
friend double mul(cal);
friend double div(cal);
};
double add(cal m)
{
return (m.a+m.b);
}
double sub(cal m)
{
if(m.a>m.b)
return (m.a - m.b);
else
return (m.b - m.a);
}
double mul(cal m)
{
return (m.a * m.b);
}
double div(cal m)
{
return (m.b/m.a);
}
int main()
{
cal ALU(20,10);
cout<<"Add :"<<add(ALU)<<endl;
cout<<"Sub :"<<sub(ALU)<<endl;
cout<<"Mul :"<<mul(ALU)<<endl;
cout<<"div :"<<div(ALU)<<endl;
}
7. Opeartor_Overloading_Pre_increment.cpp
/*
Operator overloading: extending operation on object
Allows c++ operators to be given new definition so that they can be
used with user defined data types as with built in data type.
To write operator overloaded function we either use member functions
or friend function
syntax:
return_type operator #(arguments)
{
// code
}
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class ope
{
int a;
public:
ope(int x=0)
{
a=x;
}
void display()
{
cout<<"a :"<<a<<endl;
}
/*
++ is an unary operator and we are calling it for member function of class
so number of argument's are zero
*/
void operator ++()
{
// automatically compiler is calling this function when we are writing ++o
++a;
}
};
int main()
{
ope o(10);
o.display();
++o;
o.display();
}
8. Opeartor_Overloading_for_friend.cpp
/*
syntax:
return_type operator #(arguments)
{
// code
}
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class ope
{
int a;
public:
ope(int x=0)
{
a=x;
}
void display()
{
cout<<"a :"<<a<<endl;
}
/*
++ is an unary operator and we are calling it for friend function of class
so number of argument's are one
*/
friend void operator ++(ope&);
};
void operator ++(ope& m)
{
++m.a;
}
int main()
{
ope o(10);
o.display();
++o;
o.display();
}
9. operator_overloading_Post_increment.cpp
#include<iostream>
using namespace std;
class ope
{
int a;
public:
ope(int x=0)
{
a=x;
}
void display()
{
cout<<"a :"<<a<<endl;
}
/*
In order to differentiate b/w pre-increment and post increment we need
to use a dummy variable....
because compiler is not getting whether ++ (IN void operator ++()) is
for pre- increment or for post
increment .
*/
void operator ++(int f)
{
a++;
}
};
int main()
{
ope o(10);
o.display();
o++;
o.display();
}
10. operator_overloading_Post_increment_friend.cpp
/*
syntax:
return_type operator #(arguments)
{
// code
}
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class ope
{
int a;
public:
ope(int x=0)
{
a=x;
}
void display()
{
cout<<"a :"<<a<<endl;
}
/*
++ is an unary operator and we are calling it for friend function of class
so number of argument's is one,,, second argument is dummy variable
*/
// friend void operator ++(int f, ope&);
friend void operator ++(ope&, int f);
/*
dummy variable must be taken as second argument
*/
};
//void operator ++(int x, ope& m)
void operator ++(ope& m, int x)
{
m.a++;
}
int main()
{
ope o(10);
o.display();
o++;
o.display();
}
11. Binary_Operator_overloading.cpp
/*
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
o1 o2
a=10 a=100
b= 20 b=200
*/
#include<iostream>
using namespace std;
class bin
{
int a,b;
public:
bin(int x=0, int y=0)
{
a=x,b=y;
}
void display()
{
cout<<"a :"<<a<<" b :"<<b<<endl;
}
/*
member function , binary operator --- thus no.of argument = 1
*/
void operator +(bin m)
{
/*
bin m is directly pointing to the second object(o2) ,,,, thus m.a means o2.a
and m.b means o2.b
writing only a and b will make internal link with first object...
thus writing a means o1.a and writing b means o2.b
*/
bin o3;
o3.a = a+m.a;
o3.b = b+m.b;
cout<<"na :"<<o3.a<<" b :"<<o3.b<<endl;
}
};
int main()
{
bin o1(10,20);
bin o2(100,200);
o2.display();
o1.display();
o1+o2;
}
12. Return_Binary_Operator_overloading.cpp
/*
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class bin
{
int a,b;
public:
bin(int x=0, int y=0)
{
a=x,b=y;
}
void display()
{
cout<<"a :"<<a<<" b :"<<b<<endl;
}
/*
member function , binary operator --- thus no.of argument = 1
*/
bin operator +(bin m)
{
bin o3;
o3.a = a+m.a;
o3.b = b+m.b;
return o3;
}
};
int main()
{
bin o1(10,20);
bin o2(100,200);
o2.display();
o1.display();
bin o4 = o1+o2;
o4.display();
}
13. Binary_Operator_overloading_friend.cpp
/*
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
o1 o2
a=10 a=100
b= 20 b=200
*/
#include<iostream>
using namespace std;
class bin
{
int a,b;
public:
bin(int x=0, int y=0)
{
a=x,b=y;
}
void display()
{
cout<<"a :"<<a<<" b :"<<b<<endl;
}
/*
friend function , binary operator --- thus no.of argument = 2
*/
friend bin operator +(bin ,bin);
};
bin operator +(bin m1, bin m2)
{
bin o3;
o3.a = m1.a+m2.a; // m1.a means o1.a and m2.a means o2.a
o3.b = m1.b+m2.b;
return o3;
}
int main()
{
bin o1(10,20);
bin o2(100,200);
o2.display();
o1.display();
bin o4 = o1+o2;
o4.display();
}
----------------------------------- jbu ---------------------------------------------------------------------------------
------------------------------- february27, 2018 -----------------------------------------------------------
1. Bitwisw_NotOperator_overloading.cpp
/*
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class bin
{
int a;
public:
bin(int x=0)
{
a=x;
}
void display()
{
cout<<"a :"<<a<<endl;
}
/*
member function , unary operator --- thus no.of argument = 0
*/
// void operator ~()
// {
// a = ~a;
//
// }
/*
friend function, unary operator --- thus no. of argument = 1
*/
friend void operator ~(bin&);
};
void operator ~(bin& m)
{
m.a = ~m.a;
}
int main()
{
bin o1(16);
/*
because 1's compliment of 16 = 01111
and 2's compliment of 01111 = 10001 which is equal to
17 and 17 = -17
for - sign go through signed and unsigned modifier.
*/
o1.display();
~o1;
o1.display();
}
2. Bitwise_OR_operator_overloading.cpp
/*
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class bitWiseOR
{
int a;
public:
bitWiseOR(int x=0)
{
a=x;
}
void display()
{
cout<<"a :"<<a<<endl;
}
/*
member function , binary operator --- thus no.of argument = 1
// member function -----
bitWiseOR operator |(bitWiseOR m)
{
bitWiseOR o3;
o3.a = a|m.a;
return o3;
}
*/
/*
friend function , binary operator --- thus no.of argument = 2
*/
friend bitWiseOR operator |(bitWiseOR& ,bitWiseOR&);
};
bitWiseOR operator |(bitWiseOR& m1, bitWiseOR& m2)
{
bitWiseOR o3;
o3.a = m1.a|m2.a;
return o3;
}
int main()
{
bitWiseOR o1(10);
bitWiseOR o2(6);
o1.display();
o2.display();
bitWiseOR o4 = o1|o2;
o4.display();
}
3. Real&IMG_overloading.cpp
/*
UNARY BINARY
member 0 argument 1 argument
friend 1 argument 2 argument
*/
#include<iostream>
using namespace std;
class bin
{
int real,img;
public:
bin(int x=0, int y=0)
{
real=x,img=y;
}
void display()
{
cout<<"A+jB :"<<real<<" + j"<<img<<endl;
}
/*
member function , binary operator --- thus no.of argument = 1
*/
bin operator +(bin m)
{
bin o3;
o3.real = real + m.real;
o3.img = img + m.img;
return o3;
}
};
int main()
{
bin o1(5,10);
bin o2(10,20);
bin o4 = o1+o2;
o4.display();
}
4. string.cpp
/*
string : is a predefined class which is used to declare variable
as string
*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1;
string s2;
cout<<"nEnter two stringn";
cin>>s1;
cin>>s2;
//int len = strlen(s1);
/*
we can not do this because s1 not a
variable its object
*/
int len = s1.size();
int len1 = s2.size();
cout<<"length of s1 :"<<len<<endl;
cout<<"Length of s2 :"<<len1<<endl;
/* s2 is an object */
}
5. String_function.cpp
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1;
string s2;
cout<<"nEnter two stringn";
cin>>s1;
cin>>s2;
string s3;
//string copy
s3=s1;
cout<<"s3 :"<<s3<<endl;
//string concatenation
s3=s1+"_"+s2;
cout<<"s3 :"<<s3<<endl;
}
6. String_comparison.cpp
/*
if s1 === s2 ,result = 0
if s1>s2 , result = 1
if s1<s2 , result = -1
*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1;
string s2;
cout<<"Enter two string :n";
cin>>s1>>s2;
int res = s1.compare(s2);
/* compare by ascii value thus cranes is greater than Cranes
case ignoring method is not their is c++
*/
if(res == 0)
cout<<"string have same datan";
else if(res == 1)
cout<<"s1 = "<<s1<< " is greatern";
else
cout<<"s2 = "<<s2<< " is greatern";
}
7. My_string_Overload.cpp : assignment question to perform overloading on string function
#include<iostream>
#include<cstring>
using namespace std;
class My_string
{
char s1[20],s2[20];
public:
My_string(const char *x=NULL, const char *y=NULL)
{
strcpy(s1,x);
strcpy(s2,y);
}
friend void operator +(My_string , My_string );
friend void operator ==(My_string, My_string);
//friend void operator !=(My_string, My_string);
//friend void opeartor =(My_string,My_string);
void operator =(My_string m1)
{
strcpy(s1, m1.s1);
strcpy(s2, m1.s2);
cout<<"m1.s1 :"<<m1.s1<<"t"<<"m1.s2 :"<<m1.s2<<endl;
}
};
//void operator =(My_string m1, My_string m2)
//{
// strcpy(m2.s1, m1.s1);
// strcpy(m2.s2, m1.s2);
//
// cout<<"m1.s1 :"<<m1.s1<<"t"<<"m1.s2 :"<<m1.s2<<endl;
// cout<<"m2.s1 :"<<m2.s1<<"t"<<"m2.s2 :"<<m2.s2<<endl;
//
//
//}
//void operator !=(My_string m1, My_string m2)
//{
// //int res = (m1.s1).compare(m2.s1);
// /*
// can not perform because s1 and s2 are variable not the object
// */
//
// int res = strcmp(m1.s1,m2.s1);
// if(res == 0)
// cout<<"string have same datan";
// else if(res == 1)
// cout<<"m1.s1 = "<<m1.s1<< " is greatern";
// else
// cout<<"m2.s1 = "<<m2.s1<< " is greatern";
//
// // ------------------------------------------------------
//
// int res1 = strcmp(m1.s2,m2.s2);
// if(res1 == 0)
// cout<<"string have same datan";
// else if(res1 == 1)
// cout<<"m1.s2 = "<<m1.s2<< " is greatern";
// else
// cout<<"m2.s2 = "<<m2.s2<< " is greatern";
//
//}
//
//
//
void operator ==(My_string m1, My_string m2)
{
//int res = (m1.s1).compare(m2.s1);
/*
can not perform because s1 and s2 are variable not the object
*/
int res = strcmp(m1.s1,m2.s1);
if(res == 0)
cout<<"string have same datan";
else if(res == 1)
cout<<"m1.s1 = "<<m1.s1<< " is greatern";
else
cout<<"m2.s1 = "<<m2.s1<< " is greatern";
// ------------------------------------------------------
int res1 = strcmp(m1.s2,m2.s2);
if(res1 == 0)
cout<<"string have same datan";
else if(res1 == 1)
cout<<"m1.s2 = "<<m1.s2<< " is greatern";
else
cout<<"m2.s2 = "<<m2.s2<< " is greatern";
}
void operator +(My_string m1, My_string m2)
{
char ch1[30],ch2[30];
strcpy(ch1,m1.s1);
strcat(ch1,m2.s1);
strcpy(ch2,m1.s2);
strcat(ch2,m2.s2);
cout<<"ch1 :"<<ch1<<" ch2 :"<<ch2<<endl;
}
int main()
{
My_string obj1("Cranes","banglore");
My_string obj2("varsity","banglore");
obj1 + obj2; // string cat
obj1 == obj2; // string cmp
//obj1 != obj2;
obj1 = obj2; // assigning one string into another
/*
we can overload any operator except the 1:sizeof operator, 2:typedef operator
3: dot(.) operator , 4: conditional operator(?, :):because we can use max two
operator but in ternary or conditional operator we need three operator
5: (.*) operator
*/
}
8. read_space_In_string.cpp
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string s1;
getline(cin,s1);
cout<<s1<<endl;
}
------------------------------------------------ february28, 2018 ------------------------------------------------------------
1. Inheritance.cpp
/*
Inheritance: Mechanism of reusing and extending existing classes
without modifying them.
1. newly created classes ----> derived classes/sub classes/ child classes
2. existing class -----> base class/super class/parent class
--------------------------------------------
why inheritance:
1.re usability is main advantages
2. easy debugging
3. extandibility
--------------------------------------------
private attributes of class are inherited but inaccessible in inherited
class
protected attributes of class are inherited as well as accessible in inherited
class
---------------------------------------------
syntax:
class Base_class_name
{
---
};
class derived_class_name: mode Base_class_name
{
---
};
when we call derived class object it will automatically call to base class
--------------
A derived class inherits all base class functions with the following exceptions:
1. constructor, destructor and copy constructor of the base class
2. overloaded operators of the base class
3. the friend function of the base class
----------------------------------
mode:
1.public
2.private
3.protected
*********************
private data member can not be accessed in derived class
only public and protected data members can be accessed
----------------------------------
Types of inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical inheritance: see diagram from note book
4. Multiple Inheritance
5. Hybrid/Diamond inheritance
*************
single inheritance: only one class can be derived from base class
class base
{
};
class derived : mode base
{
};
*************
Multilevel Inheritance:
BASE <----- derived1 <------ derived2
derived1 is inheriting from base class,,,, and
derived2 is inheriting from derived1 ,,,, means derived2 class
have data member of both base class as well as derived1 class
class base
{
};
class derived1: mode base
{
};
class derived2: mode base
{
};
*/
2. Inheritance_demo.cpp
#include<iostream>
using namespace std;
class base
{
public:
base() //3.
{
cout<<"In base default constructorn";
}
~base() //7.
{
cout<<"In base destructorn";
}
};
class derived: public base
{
public:
derived()
//2. it will arrive here but before printing this it will goto base default const.
{
//4.
// in case of constructor ,,, base class const.will get call first and then
// derived class constructor will get call.
cout<<"In derived default constructorn";
}
~derived() //6.
{
// in case of destructor ,,,, derived class destructor will get call first
// and then the base class destructor will get call
cout<<"In derived destructorn";
}
};
int main()
{
//1.
cout<<"in main calling default constructor using object of derived classn";
derived obj;
cout<<"In mainn"; //5.
}
3. Inheriting_dataMemberFrom_baseTOderivedClass.cpp
#include<iostream>
#include<cstring>
using namespace std;
class student
{
protected:
char name[20];
float fees;
int id;
public:
student()
{
cout<<"in base constructorn";
}
~student()
{
cout<<"In base destructorn";
}
};
class eng_student:public student
{
char collg_name[20];
public:
eng_student(const char* clg, const char* nam, float f, int i)
{
strcpy(collg_name,clg);
strcpy(name,nam);
fees = f;
id = i;
}
void display()
{
cout<<"----- In display -----n";
cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl;
}
~eng_student()
{
cout<<"in derived destructorn";
}
};
int main()
{
cout<<"inside main,,, creating object for derived classn";
eng_student obj("techno","Narayan", 45000, 1);
cout<<"in main ,,, calling displayn";
obj.display();
}
4. Inheritance_Initializing_parameterized_constructorofBASEclass.cpp
/*
In inheritance ,,, we can not call directly parameterized
constructor of base class in main
thus need to use list
*/
#include<iostream>
#include<cstring>
using namespace std;
class student
{
protected:
char name[20];
float fees;
int id;
public:
student(const char* nam, float f, int i)
{
cout<<"in base constructorn";
strcpy(name,nam);
fees = f;
id = i;
}
~student()
{
cout<<"In base destructorn";
}
};
class eng_student:public student
{
char collg_name[20];
public:
// eng_student(const char* clg)
// {
// strcpy(collg_name,clg);
//
// }
eng_student(const char* clg):student("varun",45000,2)
{
strcpy(collg_name,clg);
cout<<"In derived constructorn";
}
void display()
{
cout<<"----- In display -----n";
cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl;
}
~eng_student()
{
cout<<"in derived destructorn";
}
};
int main()
{
cout<<"in mainn";
// student obj("Narayan", 45000, 1);
eng_student obj("techno");
cout<<"in main ,,, calling displayn";
obj.display();
}
5. Inheritance_Initializing_parameterized_constructorofBASEclass_1.cpp
#include<iostream>
#include<cstring>
using namespace std;
class student
{
protected:
char name[20];
float fees;
int id;
public:
student(const char* nam, float f, int i)
{
cout<<"nnin base constructor initializing the data membern";
strcpy(name,nam);
fees = f;
id = i;
}
~student()
{
cout<<"In base destructorn";
}
};
class eng_student:public student
{
char collg_name[20];
public:
eng_student(const char* clg,const char* nam, float f, int i):student(nam,f,i)
{
cout<<"In derived constructor,, copying clg namen";
strcpy(collg_name,clg);
}
void display()
{
cout<<"----- In display -----n";
cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl;
}
~eng_student()
{
cout<<"in derived destructorn";
}
};
int main()
{
cout<<"in mainn";
eng_student obj1("techno","Narayan",45000,1);
cout<<"in main ,,, calling display for obj1n";
obj1.display();
eng_student obj2("techno","varun",45000,2);
cout<<"in main ,,, calling display for obj2n";
obj2.display();
/*
destructor will get call twice when obj1 and obj2 will go out of scope
*/
}
6. Inheritance_Copy_constructor.cpp
#include<iostream>
#include<cstring>
using namespace std;
class student
{
protected:
char name[20];
float fees;
int id;
public:
student()
{
}
student(const char* nam, float f, int i)
{
cout<<"nnin base constructor initializing the data membern";
strcpy(name,nam);
fees = f;
id = i;
}
~student()
{
cout<<"In base destructorn";
}
};
class eng_student:public student
{
char collg_name[20];
public:
eng_student()
{
}
eng_student(const char* clg,const char* nam, float f, int i):student(nam,f,i)
{
cout<<"In derived constructor,, copying clg namen";
strcpy(collg_name,clg);
}
eng_student(eng_student& m)
{
cout<<"nnIn copy constructor n";
strcpy(collg_name, m.collg_name);
strcpy(name, m.name);
fees = m.fees;
id = m.id;
}
void display()
{
cout<<"----- In display -----n";
cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl;
}
~eng_student()
{
cout<<"nn************** In derived destructor *************n";
}
};
int main()
{
cout<<"in mainn";
eng_student obj1("techno","Narayan",45000,1);
cout<<"in main ,,, calling display for obj1n";
obj1.display();
eng_student obj2=obj1;
cout<<"in main ,,, calling display for obj2n";
obj2.display();
/*
destructor will get call twice when obj1 and obj2 will go out of scope
*/
}
7. Inheritance_fruit.cpp
#include<iostream>
#include<cstring>
using namespace std;
class fruit
{
protected:
int siz;
char color[20];
public:
fruit()
{
}
fruit(int sizee, const char *clr)
{
cout<<"nnIn base constructor initializing size and colorn";
siz = sizee;
strcpy(color,clr);
}
~fruit()
{
cout<<"In base destructorn";
}
};
class Apple:public fruit
{
float cost;
public:
Apple()
{
}
Apple(float cst,int sizz, const char *clrr):fruit(sizz,clrr)
{
cout<<"in derived constructor ,,, initializing costn";
cost = cst;
}
Apple(Apple& m)
{
cout<<"In copy constructornnn";
cost = m.cost;
siz = m.siz;
strcpy(color, m.color);
}
~Apple()
{
cout<<"in derived destructorn";
}
void display()
{
cout<<"In displayn";
cout<<siz<<" "<<color<<" "<<cost<<endl;
}
};
int main()
{
cout<<"in mainn";
Apple obj1(50,2,"red");
cout<<"calling display for obj1n";
obj1.display();
Apple obj2 = obj1;
cout<<"calling display for obj2n";
obj2.display();
}
--------------------- March1, 2018 -its the occassion of holy -------------------------------------
1. Multilevel_Inheritance.cpp
#include<iostream>
#include<cstring>
using namespace std;
class student
{
protected:
char name[20];
int id;
public:
student()
{
}
Student(name,id)
Eng_student(project)
Comp_student(branch ,
marks)
student(const char* nam, int i)
{
cout<<"nnin base constructor ,, initializing name and id of studentn";
strcpy(name,nam);
id = i;
}
~student()
{
cout<<"In base class destructorn";
}
};
class eng_student:public student
{
protected:
char project[20];
public:
eng_student()
{
}
eng_student(const char *nam, int i, const char* proj):student(nam,i)
{
cout<<"In derived1 initializing project namen";
strcpy(project,proj);
}
~eng_student()
{
cout<<"In derived1 destructorn";
}
void display1()
{
cout<<name<<" "<<id<<" "<<project<<endl;
}
};
class comp_student:public eng_student
{
char brachname[20];
float marks;
public:
comp_student()
{
}
comp_student(const char* nam, int i, const char* proj, const char* brname,float
mrks):eng_student(nam,i,proj)
{
cout<<"In derived2 constructor , initialing branch name and marksn";
strcpy(brachname,brname);
marks = mrks;
}
comp_student(comp_student& m)
{
cout<<"In derived2 copy constructorn";
strcpy(name,m.name);
id = m.id;
strcpy(project, m.project);
strcpy(brachname, m.brachname);
marks = m.marks;
}
void display()
{
cout<<"*************** In display function ******************n";
cout<<"-- name id project branchname marks n";
cout<<"--"<<name <<" "<<id<<" "<<project<<" "<<brachname<<" "<<marks<<endl;
}
~comp_student()
{
cout<<"nnIn derived2 destructorn";
}
};
int main()
{
cout<<"In main,, creating object for derived1 class n";
eng_student obj1("Narayan",1, "C++");
obj1.display1();
cout<<"************************************************n";
cout<<"in main,, creating object for derived2 classn";
comp_student obj2("Narayan",1,"C++", "ECE",80);
cout<<"In main , calling display functionn";
obj2.display();
cout<<"********** in main ,,, creating copy constructor fro obj2n";
comp_student obj3 = obj2;
obj3.display();
}
2. Hierarchical_inheritance.cpp
student
Eng_student Med_student
Ece_student Cs_student Ayurvedic Homeopathic
#include<iostream>
#include<cstring>
using namespace
std;
class student
{
protected:
char name[20];
int id;
public:
student()
{
}
student(const char* nam, int i)
{
cout<<"nnIn base constructor initializing name and idn";
strcpy(name,nam);
id = i;
}
~student()
{
cout<<"In base destructorn";
}
};
class eng_student:public student
{
protected:
char clgname[30];
public:
eng_student()
{
}
eng_student(const char* clg, const char* nam,int i):student(nam,i)
{
cout<<"In eng_student initializing clg_namen";
strcpy(clgname, clg);
}
~eng_student()
{
cout<<"In eng_student destructorn";
}
};
class ece_engg:public eng_student
{
char ece_enggcourse[20];
public:
ece_engg()
{
}
ece_engg(const char* ece_course,const char* clg, const char* nam,int i):eng_student(clg,nam,i)
{
cout<<"IN ece_eng initializing coursen";
strcpy(ece_enggcourse,ece_course);
}
void display_ece()
{
cout<<"*************** in ece_eng display************n";
cout<<name<<" "<<id<<" "<<clgname<<" "<<ece_enggcourse<<endl;
}
~ece_engg()
{
cout<<"in ece_engg destructorn";
}
};
class comp_engg: public eng_student
{
char cs_course[20];
public:
comp_engg()
{
}
comp_engg(const char* cs_cours,const char* clg, const char* nam,int i):eng_student(clg,nam,i)
{
cout<<"in comp_eng constructor initializing cs_coursen";
strcpy(cs_course,cs_cours);
}
void display_comp()
{
cout<<"****************In cs displayn";
cout<<name<<" "<<id<<" "<<clgname<<" "<<cs_course<<endl;
}
~comp_engg()
{
cout<<"in comp_eng destructorn";
}
};
class med_student:public student
{
protected:
char med_clg[30];
public:
med_student()
{
}
med_student(const char* clg, const char* nam,int i):student(nam,i)
{
cout<<"nnnin med_student initializing med_clg namen";
strcpy(med_clg,clg);
}
~med_student()
{
cout<<"in med_student destructorn";
}
};
class Ayurvedic:public med_student
{
char ayr_course[20];
public:
Ayurvedic()
{
}
Ayurvedic(const char* crs, const char*clg, const char* nam, int i):med_student(clg,nam,i)
{
cout<<"in ayurvedic constructor initilaizing course namen";
strcpy(ayr_course,crs);
}
void display_ayurved()
{
cout<<"nnnn**************in ayurvedic displayn";
cout<<name<<" "<<id<<" "<<med_clg<<" "<<ayr_course<<endl;
}
~Ayurvedic()
{
cout<<"in ayurvedic destructorn";
}
};
class Homeopathic:public med_student
{
char hom_course[20];
public:
Homeopathic()
{
}
Homeopathic(const char* crs, const char*clg, const char* nam, int i):med_student(clg,nam,i)
{
cout<<"nnIn homeopathic constructor initializing coursen";
strcpy(hom_course,crs);
}
void display_hom()
{
cout<<"nnnn**************in homeopath displayn";
cout<<name<<" "<<id<<" "<<med_clg<<" "<<hom_course<<endl;
}
~Homeopathic()
{
cout<<"In homeopathic destructorn";
}
};
int main()
{
cout<<"nnnn*****************in main,,, creating object for ece_studentn";
ece_engg obj1("digital_ele","techno","narayan",1);
cout<<"nn in main calling display for ece_studentn";
obj1.display_ece();
cout<<"nnnn*****************in main,,, creating object for cs_studentn";
comp_engg obj2("networking","techno","vansh",2);
cout<<"nn in main calling display for cs_studentn";
obj2.display_comp();
cout<<"nnnn*****************in main,,, creating object for ayurved_studentn";
Ayurvedic obj3("ayurved","ayurvedic_clc","ramesh",3);
cout<<"nn in main calling display for ayurvedic_studentn";
obj3.display_ayurved();
cout<<"nnnn*****************in main,,, creating object for homeopath_studentn";
Homeopathic obj4("homeo","homepathic_clg","mahesh",4);
cout<<"nn in main calling display for homeopathic_studentn";
obj4.display_hom();
}
3. Multiple_inheritance.cpp
#include<iostream>
#include<cstring>
using namespace std;
Ece_engg Cs_engg
embedded
class ece_engg
{
protected:
char ec_branch[20];
char ec_project[20];
public:
ece_engg(const char* brch, const char* proj)
{
cout<<"nnnIn ece_engg constructor initializing branch and projectn";
strcpy(ec_branch,brch);
strcpy(ec_project,proj);
}
~ece_engg()
{
cout<<"in ece_engg destructorn";
}
};
class cs_engg
{
protected:
char cs_branch[20];
char cs_project[20];
public:
cs_engg(const char *brch, const char* proj)
{
cout<<"nnn in cs_engg constructor initializing branch and projectn";
strcpy(cs_branch,brch);
strcpy(cs_project,proj);
}
~cs_engg()
{
cout<<"in cs_engg destructorn";
}
};
class embedded:public ece_engg, public cs_engg
{
char subject[20];
float marks;
public:
embedded(const char* sub, float mrks,const char* proj):ece_engg("ECE",proj),cs_engg("CSE",proj)
{
cout<<"in embedded constructor ,initializing subject and marksn";
strcpy(subject,sub);
marks = mrks;
}
void display_cs()
{
cout<<"nn***************** in cs displayn";
cout<<cs_branch<<" "<<cs_project<<" "<<subject<<" "<<marks<<endl;
}
void display_ece()
{
cout<<"nn***************** in ece displayn";
cout<<ec_branch<<" "<<ec_project<<" "<<subject<<" "<<marks<<endl;
}
~embedded()
{
cout<<"nnin embedded destructorn";
}
};
int main()
{
cout<<"In main creating onject for embeddedn";
embedded obj("microprocessor",82, "c++");
obj.display_cs();
obj.display_ece();
}
4. virtual_in_inheritance.cpp
/*
**********************************
order of execution is from left to right
use of virtual keyword can change order of execution
**********************************
class base1
{};
class base2
{};
class der: public base1, public base2
{};
----------------------
order of execution : base1 constructor, base2 constructor,der constructor
der destructor, base2 destructor, base1 destructor
----------------
class der: public base2, public base1
{};
----------------------
order of execution : base2 constructor, base1 constructor,der constructor
der destructor, base1 destructor, base2 destructor
----------------
class der: public base1, virtual public base2
{};
----------------------
order of execution : base2 constructor, base1 constructor,der constructor
der destructor, base1 destructor, base2 destructor
----------------
class der:virtual public base1, public base2
{};
----------------------
order of execution : base1 constructor, base2 constructor,der constructor
der destructor, base2 destructor, base1 destructor
----------------
class der: virtual public base1, virtual public base2
{};
----------------------
order of execution : base1 constructor, base2 constructor,der constructor
der destructor, base2 destructor, base1 destructor
----------------
if.....
class der:base1, virtual base2, base3
{};
order of execution: base2 constructor, base1 constructor,base3 constructor,
der constructor
--- der destructor, base3 destructor, base1 destructor, base2 constructor
*/
#include<iostream>
using namespace std;
class base1
{
public:
base1()
{
cout<<"base1 default constructorn";
}
~base1()
{
cout<<"base1 destructorn";
}
};
class base2
{
public:
base2()
{
cout<<"base2 default constructorn";
}
~base2()
{
cout<<"base2 destructorn";
}
};
//class der:public base1,public base2
//class der:public base1,public base2
class der:public base1,virtual public base2
//class der:virtual public base1,public base2
//class der:virtual public base1,virtual public base2
//class der:public base1,virtual public base2
{
public:
der()
{
cout<<"derived default constructorn";
}
~der()
{
cout<<"derived destructorn";
}
};
int main()
{
der d;
}
5. deathOfDiamond_OR_VirtualBaseClassConcept.cpp
/*
final derived class have two copies of base class ,,, thus compiler is ambiguous
this is known as death of diamond
-----
use of virtual keyword will avoid this problem ,,, as it allows only single copy
of base class in derived class
*/
#include<iostream>
using namespace std;
class person
{
public:
void show()
{
cout<<"inside personn";
}
};
//class men:public person
class men:virtual public person
{
};
//class women:public person
class women:virtual public person
{
};
class child:public men,public women
{
};
int main()
{
child c;
c.show();
}
6. virtual_ASSignmentPage88.cpp
#include<iostream>
#include<cstring>
using namespace std;
class student
{
protected:
char name[10];
int id;
public:
student()
{
}
// student()
// {
// strcpy(name,"narayan");
// id = 1;
// }
student(const char* nam, int i)
{
cout<<"nnIn base class student constructorn";
strcpy(name,nam);
id = i;
}
~student()
{
cout<<"in base student destructorn";
}
};
class ece_engg:virtual public student
{
protected:
char ec_branch[20];
char ec_project[20];
public:
ece_engg(const char* brch, const char* proj)
{
cout<<"nnnIn ece_engg constructor initializing branch and projectn";
strcpy(ec_branch,brch);
strcpy(ec_project,proj);
}
~ece_engg()
{
cout<<"in ece_engg destructorn";
}
};
class cs_engg:virtual public student
{
protected:
char cs_branch[20];
char cs_project[20];
public:
cs_engg(const char *brch, const char* proj)
{
cout<<"nnn in cs_engg constructor initializing branch and projectn";
strcpy(cs_branch,brch);
strcpy(cs_project,proj);
}
~cs_engg()
{
cout<<"in cs_engg destructorn";
}
};
class embedded:public ece_engg, public cs_engg
{
char subject[20];
float marks;
public:
embedded(const char* sub, float mrks,const char* proj,const char* nam,int i):
ece_engg("ECE",proj), cs_engg("CSE",proj), student(nam,i)
{
cout<<"in embedded constructor ,initializing subject and marksn";
strcpy(subject,sub);
marks = mrks;
}
void display_cs()
{
cout<<"nn***************** in cs displayn";
cout<<name<<" "<<id<<" "<<cs_branch<<" "<<cs_project<<" "<<subject<<" "<<marks<<endl;
}
void display_ece()
{
cout<<"nn***************** in ece displayn";
cout<<name<<" "<<id<<" "<<ec_branch<<" "<<ec_project<<" "<<subject<<" "<<marks<<endl;
}
~embedded()
{
cout<<"nnin embedded destructorn";
}
};
int main()
{
cout<<"In main creating onject for embeddedn";
embedded obj("microprocessor",82, "c++","Narayan",1);
obj.display_ece();
obj.display_cs();
}
-------------------------------------------------- March 2, 2018 --------------------------------------------------------------
1.functionOverriding.cpp
2.UpCasting.cpp
3.DownCasting.cpp
1. functionOverriding.cpp
/*
The facility of writing functions with same name,arguments, and return type in
both the base class and derived class.
*/
#include<iostream>
using namespace std;
class base
{
int a;
public:
base()
{
a=10;
}
void show()
{
cout<<"In base t"<<"a :"<<a<<endl;
}
};
class der:public base
{
int b;
public:
der()
{
b=20;
}
void show()
{
cout<<"In derived t"<<"b :"<<b<<endl;
}
};
int main()
{
base b;
b.show();
der d;
d.show();
}
2. UpCasting.cpp
/*
Upcasting: base class pointer pointing to derived class object is technically
termed as Upcasting.
base_ptr = &base_obj;
base_ptr = &der1_obj;
base_ptr = &der2_obj;
polymorphism: single interface, multiple implementation(example show(): same name but used in
both base class and derived class )
compile time polymorphism(function will be called during compile time): function overloading ,
operator overloading
runtime polymorphism: virtual function, late binding
late binding : when using virtual keyword b->show() will call to derived class
show() function during runtime ,,, this is called late binding
BASE
Derived1
Derived2
early binding: writing b->print() will call to base class function ,, during compile time known as early
binding...
*/
#include<iostream>
using namespace std;
class base
{
int a;
public:
base()
{
a=10;
}
void print()
{
cout<<"In print:: Basen";
}
// void show()
// {
// cout<<"In base t"<<"a :"<<a<<endl;
// }
virtual void show()
{
cout<<"In base t"<<"a :"<<a<<endl;
}
};
class der:public base
{
int b;
public:
der()
{
b=20;
}
void show()
{
cout<<"In derived t"<<"b :"<<b<<endl;
}
void print()
{
cout<<"In print::derivedn";
}
};
int main()
{
//base b.show(); not allowed
base *b;
der d;
b = &d; // Upcasting
b->show(); //late binding
b->print(); //early binding
}
3. DownCasting.cpp
/*
DownCasting : derived class pointer pointing to base class object is illegal and
and it technically termed as downcasting.
*/
#include<iostream>
using namespace std;
class base
{
int a;
public:
base()
{
a=10;
}
void print()
{
cout<<"In print:: Basen";
}
// void show()
// {
// cout<<"In base t"<<"a :"<<a<<endl;
// }
virtual void show()
{
cout<<"In base t"<<"a :"<<a<<endl;
}
};
class der:public base
{
int b;
public:
der()
{
b=20;
}
void show()
{
cout<<"In derived t"<<"b :"<<b<<endl;
}
void print()
{
cout<<"In print::derivedn";
}
};
int main()
{
base b;
der *d;
d = &b;
d->show();
d->print();
}
------------------------------------ March5,2018 chand sir not came today ----------------------------------
1. abstraction_encapsulation_piggybank.cpp
2. Virtual_table.cpp ;;;; kindly google it
3.operator_overloading_for_New&delete.cpp
4.global_overloading_for_new&delete.cpp
5.operatoroverloading_withoutclass.cpp
6.pure_virtual_function.cpp
7.useof_initializerList_for_initializing_constVariable.cpp
8.pure_virtual_assignment.cpp
1. abstraction_encapsulation_piggybank.cpp
#include<iostream>
using namespace std;
class piggybank
{
private:
int rs5;
int rs10;
int rs20;
int rs5_total, rs10_total, rs20_total,piggybnak_total;
public:
piggybank(int rs5 , int rs10, int rs20)
{
cout<<"nnnIn piggybank constructor getting notesn";
this->rs5 = rs5;
this->rs10 = rs10;
this->rs20 = rs20;
}
void total_amount()
{
cout<<"calculating total amountn";
rs5_total = rs5 * 5;
rs10_total = rs10 * 10;
rs20_total = rs20 * 20;
piggybnak_total = rs5_total+rs10_total+rs20_total;
}
void display()
{
cout<<"in displayn";
cout<<"rs5 total amount :"<<rs5_total<<"n";
cout<<"rs10 total amount :"<<rs10_total<<"n";
cout<<"rs20 total amount :"<<rs20_total<<"n";
cout<<"total amount in piggy bank :"<<piggybnak_total<<"n";
}
~piggybank()
{
cout<<"In piggybank destructorn";
}
};
int main()
{
int rs5,rs10,rs20;
cout<<"Enter total number of notes of rs5,rs10 and rs20n";
cin>>rs5>>rs10>>rs20;
piggybank obj(rs5,rs10,rs20);
obj.total_amount();
obj.display();
}
2. Virtual_table.cpp
/*
any class that has virtual function, a v table will be generated
this v table will hold all the virtual function
derived class has its own virtual table ,, whether we are creating or not
base *ptr = derived_obj
automatically compiler will assign 4 byte to ptr ,,, means memory will be increased by 4byte
this ptr will hold the address of derived class show() address.
*/
3. operator_overloading_for_New&delete.cpp
/*
size_t for type casting of long long int
Syntax for overloading the new operator :
void* operator new(size_t size);
The overloaded new operator receives size of type size_t, which specifies the number of bytes
of memory to be allocated. The return type of the overloaded new must be void*.
The overloaded function returns a pointer to the beginning of the block of memory allocated.
Syntax for overloading the delete operator :
void operator delete(void*);
The function receives a parameter of type void* which has to be deleted. Function should not
return anything.
NOTE: Both overloaded new and delete operator functions are static members by default.
Therefore, they do not have access to this pointer .
*/
#include<iostream>
#include<cstdlib> // c libraries can be used by writing c as prefix
using namespace std;
class student
{
private:
string name;
int age;
public:
student()
{
cout<<"in student constructorn";
}
// student(string name,int age)
// {
// this->name = name;
// this->age=age;
// }
student(string name,int age):name(name),age(age)
{
}
void display()
{
cout<<"in display functionn";
cout<<"Name :"<<name<<", Age :"<<age<<endl;
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size << endl;
//void * p = ::new student();
void * p = malloc(size); //will also work fine
return p;
}
void operator delete(void *p)
{
cout<< "Overloading delete operator " << endl;
free(p);
}
};
int main()
{
student * p = new student("Yash", 24);
p->display();
delete p;
}
4. global_overloading_for_new&delete.cpp
// CPP program to demonstrate
// Global overloading of
// new and delete operator
#include<iostream>
#include<stdlib.h>
using namespace std;
void * operator new(size_t size)
{
cout << "New operator overloading " << endl;
void * p = malloc(size);
return p;
}
void operator delete(void * p)
{
cout << "Delete operator overloading " << endl;
free(p);
}
int main()
{
int n = 5, i;
int * p = new int[3];
for (i = 0; i<n; i++)
p[i]= i;
cout << "Array: ";
for(i = 0; i<n; i++)
cout << p[i] << " ";
cout << endl;
delete p;
}
5. pure_virtual_function.cpp
/*
pure virtual function is a function which does not have a body , and
is always equating to zero.
example
virtual void show() = 0;
any class which has at least one virtual function then that particular class will called as
abstract base class ,,, and those abstract class are not allowed to create its object.
usage:
size of an object = size of non_static member + size of virtual function
*/
#include<iostream>
using namespace std;
class Base
{
protected:
int x;
public:
virtual void fun() = 0; // pure virtual function
Base(int i)
{
x=i;
}
};
class Derived: public Base // this class inherits from base and implements fun()
{
int y;
public:
Derived(int i,int j):Base(i) // initializer list allowed us to initialize const and reference
{
y=j;
}
void fun()
{
cout<<"fun() calledn";
cout<<"x :"<<x<<" y :"<<y<<endl;
}
};
int main()
{
cout<<sizeof(class Base)<<endl;
Derived d(10,20);
d.fun();
// Base obj; // cannot declare variable 'obj' to be of abstrct type 'Base'
/*
pointer to abstract class can be created but creating obj of base class is not allowed
*/
Base *ptr = &d;
}
6. useof_initializerList_for_initializing_constVariable.cpp
#include<iostream>
using namespace std;
/*
class Base
{
protected:
const int x;
public:
virtual void fun() = 0; // pure virtual function
Base(int i)
{
x=i;
}
};
*/
/*
if a variable is declared as const then it have to be initialized at the time of declaration ,,
otherwise it will take any garbage value ,,, and it can not be initialized further
but using the initializer list a const variable can be initializing even after the declaration
*/
class Base
{
protected:
const int x;
public:
virtual void fun() = 0; // pure virtual function
Base(int i):x(i)
{
}
};
class Derived: public Base // this class inherits from base and implements fun()
{
int y;
public:
Derived(int i,int j):Base(i) // initializer list allowed us to initialize const and reference
{
y=j;
}
void fun()
{
cout<<"fun() calledn";
cout<<"x :"<<x<<" y :"<<y<<endl;
}
};
int main()
{
cout<<sizeof(class Base)<<endl;
Derived d(10,20);
d.fun();
}
7. pure_virtual_assignment.cpp
#include<iostream>
#include<cstring>
#include<string.h>
using namespace std;
class AbstractString
{
protected:
char str[20];
public:
AbstractString()
{
}
AbstractString(const char* s1)
{
cout<<"IN base class constructor initializing strn";
strcpy(str,s1);
}
~AbstractString()
{
}
virtual void display() =0;
};
class UpperString:public AbstractString
{
public:
UpperString()
{
}
UpperString(const char* s1):AbstractString(s1)
{
cout<<"in upper string n";
//strcpy(str,s1);
}
~UpperString()
{
}
void display()
{
cout<<"Uppercase string :"<<strupr(str);
}
};
class LowerString:public AbstractString
{
public:
LowerString()
{
}
LowerString(const char* s1):AbstractString(s1)
{
cout<<"in lowerStringn";
//strcpy(str,s1);
}
~LowerString()
{
}
void display()
{
cout<<"Lowercase string :"<<strlwr(str);
}
};
int main()
{
char s1[20],s2[20];
cout<<"Enter a lower case stringn";
cin>>s1;
UpperString objec(s1);
AbstractString *obj=NULL;
UpperString uprobj;
obj = &uprobj;
obj->display();
//***********************************************
cout<<"nnEnter a upper case stringn";
cin>>s2;
LowerString objec1(s2);
AbstractString *obj1;
LowerString lwrobj;
obj1 = &lwrobj;
obj1->display();
}
/*
----------------------------------------------- March6, 2018 ----------------------------------------------------
1.Templets.cpp
2.templates1.cpp
3.templates2.cpp
4.swap_using_templates.cpp
5.maxOf2_usingTemplates_ternary_operator.cpp
6.calculator_templates.cpp
7.calculator_templates_outside class.cpp
8.STL.cpp
9.queue.cpp
10.static_in_template.cpp
1. Templates.cpp
/*
nm tool --- for mangling
C++filt _Z3adddd ----- to check mangling
*/
/*
#include<iostream>
using namespace std;
void add(int a, int b)
{
cout<<"a :"<<a <<" ,b :"<<b<<" ,a+b :"<<a+b<<endl;
}
void add(double a, double b)
{
cout<<"a :"<<a<< " ,b :"<<b<<" ,a+b :"<<a+b<<endl;
}
int main()
{
add(2,3);
add(1.2,2.3);
}
*/
/*
Templates: instead of writing add() multiple times we can write it only once and
executes many times for different data type.(means re usability purpose)
templates allows us to perform generic programming.
Templates are of two type : standard template library and user defined templates
STL : have various classes
1. QUeue
2.List
3.vector
4.map
user defined templates:
1.function
2.class template
*/
#include<iostream>
using namespace std;
template<class T> // here class is a keyword which works as place holder for data type
void add(T a, T b)
{
cout<<"a :"<<a<<" b :"<<b<<" a+b :"<<a+b<<endl;
}
int main()
{
add(1,2);
add(1.2,2.3);
}
2. templates1.cpp
#include<iostream>
using namespace std;
template<class T, typename T1> // here class is a keyword which works as place holder for data
type
void add(T a, T1 b)
{
cout<<"a :"<<a<<" b :"<<b<<" a+b :"<<a+b<<endl;
}
int main()
{
add(1,2);
add(11.2,12.3);
}
3. templates2.cpp
#include<iostream>
using namespace std;
template<class T, typename T1> // here class is a keyword which works as place holder for data
type
T1 add(T a, T1 b)
{
return (a+b);
}
int main()
{
cout<<"res :"<<add<double,double>(11.2,12.3)<<endl;
cout<<"res :"<<add<int>(11,12)<<endl;
cout<<"res :"<<add<char>(11,'a')<<endl; // implicit conversion
}
4. swap_using_templates.cpp
#include<iostream>
using namespace std;
template<class T, typename T1>
void swp(T a, T1 b)
{
T1 c;
c = a;
a = b;
b = c;
cout<<"a= "<<a<<" b = "<<b<<endl;
}
int main()
{
int a=10;
float b=12;
swp(a,b);
}
5. maxOf2_usingTemplates_ternary_operator.cpp
#include<iostream>
using namespace std;
template<class T>
T maxof2(T a, T b)
{
//(a>b)? return(a):return(b);
return (a>b?a:b);
}
int main()
{
int a=12, b=24;
cout<<"max among a :"<<a<<" ,b :"<<b<<" is :"<<maxof2(a,b)<<endl;
}
6. calculator_templates.cpp
#include<iostream>
using namespace std;
template<class T>
class calculator
{
T a,b;
public:
calculator(T x,T y):a(x),b(y)
{
cout<<"Inside constructorn";
}
~calculator()
{
cout<<"inside destructorn";
}
T add()
{
return (a+b);
}
T sub()
{
if(a>b)
{
return (a-b);
}
else
return (b-a);
}
T mul()
{
return (a*b);
}
T div()
{
return (a/b);
}
};
int main()
{ // student obj(5,2);
calculator<double> obj(5,2);
cout<<"In mainn";
cout<<"add(5,2) :"<<obj.add()<<endl;
cout<<"sub(5,2) :"<<obj.sub()<<endl;
cout<<"mul(5,2) :"<<obj.mul()<<endl;
cout<<"div(5,2) :"<<obj.div()<<endl;
}
7. calculator_templates_outside class.cpp
#include<iostream>
using namespace std;
template<class T>
class calculator
{
T a,b;
public:
calculator(T x, T y);
T add();
T sub();
T mul();
T div();
~calculator()
{
cout<<"inside destructorn";
}
};
template<class T>
calculator<T>::calculator(T x,T y)
{
cout<<"Inside constructorn";
a=x;
b=y;
}
template<class T>
T calculator<T>:: add()
{
return (a+b);
}
template<class T>
T calculator<T>::sub()
{
if(a>b)
{
return (a-b);
}
else
return (b-a);
}
template<class T>
T calculator<T>::mul()
{
return (a*b);
}
template<class T>
T calculator<T>:: div()
{
return (a/b);
}
int main()
{
// student obj(5,2);
calculator<double> obj(5,2);
cout<<"In mainn";
cout<<"add(5,2) :"<<obj.add()<<endl;
cout<<"sub(5,2) :"<<obj.sub()<<endl;
cout<<"mul(5,2) :"<<obj.mul()<<endl;
cout<<"div(5,2) :"<<obj.div()<<endl;
}
8. STL.cpp
/*
STL: standard template library
3 element: containers , iterators and algorithms
containers are something which are capable to store something.
containers are of type: total 16 containers are there.
sequential ,ordered,unordered,
-------------
iterator are used to travel through containers.
forward iterator, backward iterator,control iterator
------------------
algorithms:
*/
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vobj;
vobj.push_back(10);
vobj.push_back(20);
vobj.push_back(30);
cout<<"Displaying content of vector :n";
if(vobj.empty())
cout<<"vector is emptyn";
else
{
for(int i=0;i<vobj.size();i++) // simple iterator using for loop
{
cout<<vobj[i]<<endl;
}
}
// creating two vector with two object in heap segment(since its dynamic)
vector<char> vobj1;
vobj1.push_back('a');
vobj1.push_back('c');
vobj1.push_back('b');
cout<<"Displaying content of vector :n";
for(int i=0;i<vobj1.size();i++) // here object is indexed based
{
cout<<vobj1[i]<<endl;
}
// -------------- forward iterator : left begin----------------
cout<<"forward iteratorn";
vector<int>:: iterator lt;
for(lt = vobj.begin();lt!=vobj.end(); lt++) // here iterator is pointer based
{
cout<<*lt<<endl;
}
// -------------- reverse iterator :right begin ----------------
cout<<"reverse iteratorn";
vector<int>:: reverse_iterator rt;
for(rt = vobj.rbegin();rt!=vobj.rend(); rt++) // here iterator is pointer based
{
cout<<*rt<<endl;
}
cout<<"printing from second elementn";
for(rt = vobj.rbegin()+1;rt!=vobj.rend(); rt++) // here iterator is pointer based
{
cout<<*rt<<endl;
}
// ------------ front AND BACK
cout<<"vobj.front() :"<<vobj.front()<<" vobj1.back() :"<<vobj1.back()<<endl;
cout<<"vobj.size() :"<<vobj.size()<<endl;
vobj.pop_back();
cout<<"pop_back() iteratorn";
for(lt = vobj.begin();lt!=vobj.end(); lt++) // here iterator is pointer based
{
cout<<*lt<<endl;
}
// ------------------- sum and delete until list is not empty --------------------
cout<<"vobj.size() :"<<vobj.size()<<endl;
cout<<"sum of all element :n";
int sum = 0;
// for(lt = vobj.begin();lt!=vobj.end(); lt++) // here iterator is pointer based
// {
// sum = sum + (*lt);
//
// }
//while(vobj.size()!=0)
while(!vobj.empty())
{
sum = sum + vobj.back();
vobj.pop_back();
}
cout<<sum;
}
9. queue.cpp
/*
Adapter class
here queue is a vector class
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
void showq(queue<int> gq)
{
queue<int> g = gq;
while(!g.empty())
{
cout<<'t'<<g.front();
g.pop();
}
cout<<"n";
}
int main()
{
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
cout<<"The queue is :n";
showq(q);
cout<<"nsize :"<<q.size();
cout<<"nfront :"<<q.front();
cout<<"nback :"<<q.back();
cout<<"npop() :";
q.pop();
showq(q);
}
10. static_in_template.cpp
#include<iostream>
using namespace std;
template<class T>
class test
{
private:
T val;
public:
static int count;
test()
{
count++;
}
};
template<class T>
int test<T>::count = 0;
int main()
{
test<int> a;
test<int> b;
test<double> c;
cout<<test<int>::count <<endl;
cout<<test<double>::count <<endl;
}
----------------------------------------------------------------- END -------------------------------------------------------------
C++ Programs
C++ Programs
C++ Programs
C++ Programs
C++ Programs
C++ Programs
C++ Programs

C++ Programs

  • 1.
    ---------------------- ---------------------------- C++ module ------------------------------------------- Narayan Lal Menariya ------------------------------------------------------------------------------------------------------------------ february 20,2018 --- 1st day 1.sample1.cpp 2.namespace.cpp 3.defaultargument.cpp 4.inline.cpp 5.references.cpp 6.swapusingREFERENCES.cpp 7.references1.cpp 8.functionoverloading.cpp 9.functionoverloading1.cpp 10.structure.cpp 11. structure.c // its c program to verify difference between structure in c and c++ --------------------------------------------- february 21,2018 ----- 2nd day ------------------------------------------- 1.private.cpp 2.class_function.cpp 3.class_function1.cpp 4.class_student.cpp 5.constructor.cpp 6.param_constructor.cpp 7.student_constructor.cpp 8.Defaultcompliler_constructor.cpp
  • 2.
    9.destructor.cpp 10.parameterized_const&destructor.cpp --------------------------------------------- february 22,2018 ---------------------------------------------------- Dynamic memory allocation: 1. NewOperator.cpp 2. newfordynamicARRAY.cpp 3. memoryForObject.cpp 4. malloc.cpp 5. parameterized_constUsingDMA.cpp 6. default_paraConstUsingDMA.cpp 7. ThisPointer.cpp 8. writingconstructor&display_OUTSIDEclass.cpp 9. Deepcopy.cpp 10. shallowCopy.cpp ------------------------------------------------- february23, 2018 ----------------------------------------------------- 1.const_datamember.cpp 2.const_memberfunction.cpp 3.const_object.cpp 4.static.cpp 5.static_memberfunction.cpp 6.static_localVariable.cpp 7.newfor2DArray.cpp
  • 3.
    -------------------------------------- february26, 2018------------------------------------------------------- 1.friend.cpp 2.friend_second.cpp 3.friend_passbyvalue.cpp 4.friend_passbyReference.cpp 5.friend_InClass.cpp 6.friend_calculator.cpp 7.Opeartor_Overloading_Pre_increment.cpp 8.Opeartor_Overloading_for_friend.cpp 9.operator_overloading_Post_increment.cpp 10.operator_overloading_Post_increment_friend.cpp 11.Binary_Operator_overloading.cpp 12.Return_Binary_Operator_overloading.cpp 13.Binary_Operator_overloading_friend.cpp ----------------------------------- jbu ------------------------------------- ------------------------------- february 27,2018 ---------------------------- 1.Bitwisw_NotOperator_overloading.cpp 2.Bitwise_OR_operator_overloading.cpp 3.Real&IMG_overloading.cpp 4.string.cpp 5.String_function.cpp 6.String_comparison.cpp 7.My_string_Overload.cpp : assignment question to perform overloading on string function 8.read_space_In_string.cpp
  • 4.
    -------------------------------------- february 28,2018------------------------- 1.Inheritance.cpp 2.Inheritance_demo.cpp 3.Inheriting_dataMemberFrom_baseTOderivedClass.cpp 4.Inheritance_Initializing_parameterized_constructorofBASEclass.cpp 5.Inheritance_Initializing_parameterized_constructorofBASEclass_1.cpp 6.Inheritance_Copy_constructor.cpp 7.Inheritance_fruit.cpp ------------------------------- March1, 2018 -its the occassion of holy ----------------------- 1.Multilevel_Inheritance.cpp 2.Hierarchical_inheritance.cpp 3.Multiple_inheritance.cpp 1. Sample1.cpp #include<iostream> int main() { //std::cout<<"Hello world:n"; std::cout<<"Hello world:n"<<std::endl;; int a; std::cin>>a; // >> extraction operator ,,,,, << insertion operator std::cout<<"a :"<<a<<std::endl;
  • 5.
    // cin andcout are called objects of iostream while printf() and scanf() are functions // :: called scope of resolution operator } 2. Namespaces.cpp // namespaces are used to avoid global collision for same variable #include<iostream> //int a =10; //int a=23; // its is not allowed to use same variable for two different values /* namespace cranes { int a = 10; } namespace varsity { int a=23; } int main() {
  • 6.
    std::cout<<"cranes a:"<<cranes::a<<std::endl; std::cout<<"varsity a:"<<varsity::a<<std::endl; } */ //------------------------------------------------------------------- /* namespacecranes { int a = 10; } namespace varsity { int a=23; } using namespace varsity; // it is used to print bydefault value of second time declared variable int main() { std::cout<<"a :"<<a<<std::endl; } //---------------------------------
  • 7.
    */ using namespace std;// we are creating std as default namespace cranes { int a = 20; } namespace varsity { int a=25; } using namespace varsity; // it is used to print bydefault value of second time declared variable int main() { cout<<"a :"<<a<<endl; } 3. Defaultargument.cpp #include<iostream> using namespace std; int fun(int a, int b,int c=0, int d=0); //int fun(int a, int b,int c=0, int d); // error because assigning is from right to left int main() { cout<<fun(1,2)<<endl; // a=1,b=2 cout<<fun(1,2,3)<<endl; // a=1,b=2,c=3
  • 8.
    cout<<fun(1,2,3,4)<<endl; // a=1,b=2,c=3,d=4 } intfun(int a, int b, int c, int d) { return a+b+c+d; } 4. Inline.cpp #include<iostream> using namespace std; #define swp(a,b) a=a+b; b = a-b; a=a-b; cout<<"a:"<<a<<endl; // macros are consuming more memory but they are faster than function /* void swap(int a, int b) { a=a+b; b=a-b; a=a-b; cout<<"a= "<<a<<" b = "<<b<<endl;
  • 9.
    } */ // inline willmake function swap(10,20)to work like macro function swp(a,b) inline void swap(int a, int b) { a=a+b; b=a-b; a=a-b; cout<<"a= "<<a<<" b = "<<b<<endl; } int main() { int a=30,b=20; swp(a,b); swap(10,20); } 5. References.cpp // references are like pointer but there is no need of dereferencing // always refences should be initialized at the time of decleration // type &reference_name=variable_name; // no need of writing & before variable_name
  • 10.
    #include<iostream> using namespace std; intmain() { int a=12; //int &ref =12 // can not point to a constant value // int &ref; // is also not allowed // int &ref=NULL; // is also not allowed because NULL = 0 which is constant value int &ref = a; // no need of writing like ref = &a cout<<"ref : "<<ref<<endl; cout<<"a's address :"<<&a<<endl; cout<<"ref's address : "<<&ref<<endl; // &a and &ref are giving same value it means memory is not allocated for ref variable } 6. swapusingREFERENCES.cpp #include<iostream> using namespace std; //void swap(int a, int b) void swap(int &a, int &b) // here we are declaring and initialing a and b {
  • 11.
    a=a+b; b=a-b; a=a-b; cout<<"a= "<<a<<" b= "<<b<<endl; } int main() { int a=10,b=20; int &refA=a, &refB = b; swap(refA,refB); } 7. references1.cpp #include<iostream> using namespace std; int main() { int a=10; int &ref = a; ref++; cout<<"a ="<<a<<endl; a++; cout<<"a ="<<a<<endl;
  • 12.
    cout<<"sizeof(int&) :"<<sizeof(int&)<<endl; cout<<"sizeof(char& :)"<<sizeof(char&)<<endl; //size of reference variable varies according to datatype of variable but in pointer it is always fixed } 8. functionoverloading.cpp /* function overloading : ---features of c++ that allows multiple function to have same function name .... multiple function may have same function name either in differ with type of argument or order of argument or number of argument */ #include<iostream> using namespace std; void fun(int a, int b) { cout<<"a :"<<a <<" ,b :"<<b<<endl; } void fun(int a, int b,int c) { cout<<"a :"<<a<< " ,b :"<<b<<"c :"<<c<<endl; } //void fun(float a, int b)
  • 13.
    // will giveerror as c++ by default taking float value as double void fun(double a, int b) { cout<<"a :"<<a <<" ,b :"<<b<<endl; } void fun(int a, double b) { cout<<"a :"<<a <<" ,b :"<<b<<endl; } void fun(int a, float b) { cout<<"a :"<<a <<" ,b :"<<b<<endl; } int main() { fun(1,2); fun(2.3,57); fun(57,4.5); fun(34,3.4f); // writing f will make 3.4 float ---- if not writing f it will be considered as double fun(1,2,3); }
  • 14.
    9. functionoverloading1.cpp #include<iostream> using namespacestd; #define PI 3.14 void area(int); void area(float); void area(int,int); int main() { int side=10; float rad=12.2f; int length = 15,breadth=4; area(side); area(rad); area(length,breadth); } void area(int side_sqr) { cout<<"Area of square with side :"<<side_sqr<<" is :"<<"t"; cout<<side_sqr*side_sqr<<endl;
  • 15.
    } void area(float radius) { cout<<"areaof circle with radius = "<<radius<<" is"<<"t"; cout<<PI*radius*radius<<endl; } void area(int len,int bre) { cout<<"Area of rectangle with len="<<len<<" and"; cout<<" breadth = "<<bre<<" is :"<<"t"; cout<<len*bre<<endl; } 10. structure.cpp /* difference between structure in c and c++ ------------- page 19 1. structure in c++ contain both data and function variable while in c structure contains only data member 2. need not use the struct keyword while creating the variable but in c needed
  • 16.
    3. size ofempty structure is 1 byte in gcc compiler but in c size is 0 4. can have static data member but in c cannot have static data member 5. access specifier can be used for providing data security but in c no concept of access specifier difference between private and public */ #include<iostream> using namespace std; int fun(int a, int b); struct student { private: int rollnum; char name[10]; public: int fun(int a, int b) { return a+b; } }; struct emptystructure {
  • 17.
    }; int main() { char name[10]; introllnum; //struct student s; student s; emptystructure emp; cout<<"Enter name and roll number :"<<endl; cin>>name>>rollnum; int c = s.fun(3,4); cout<<"c :"<<c<<endl; cout<<"sizeof student :"<<sizeof(s)<<endl; cout<<"sizeof emptystructure :"<<sizeof(emp)<<endl; return 0; } /* ----------------- structure.c --------------------- #include<stdio.h> //using namespace std;
  • 18.
    struct student { //private: int rollnum; charname[10]; // public: //int fun(int a, int b) //{ // return a+b; //} }; struct emptystructure { }; int main() { char name[10]; int rollnum; //student s; // error struct student s; struct emptystructure emp; printf("%u",sizeof(emp)); //printf("Enter name and roll number :");
  • 19.
    //scanf(%s,%d,&s.name,&s.rollnum); //int c =s.fun(3,4); //printf("c :%d",c); //cout<<"sizeof student :"<<sizeof(s)<<endl; //cout<<"sizeof emptystructure :"<<sizeof(emp)<<endl; return 0; } */ 11. structure.c // its c program to verify difference between structure in c and c++ #include<stdio.h> //using namespace std; struct student { //private: int rollnum; char name[10]; // public: //int fun(int a, int b) //{
  • 20.
    // return a+b; //} }; structemptystructure { }; int main() { char name[10]; int rollnum; //student s; struct student s; struct emptystructure emp; printf("%u",sizeof(emp)); //printf("Enter name and roll number :"); //scanf(%s,%d,&s.name,&s.rollnum); //int c = s.fun(3,4); //printf("c :%d",c); //cout<<"sizeof student :"<<sizeof(s)<<endl; //cout<<"sizeof emptystructure :"<<sizeof(emp)<<endl; return 0;
  • 21.
    } ---------------------------------------- february 21,2018----- 2nd day ---------------------------------------------------- 1. private.cpp /* C++ features 1. Abstraction 2. Encapsulation 3. polymorphism 4. Inheritance */ #include<iostream> using namespace std; //class test //{ //public: // int a; // char c; // //}; //int main() //{ // //objects are instance of class // // here o1 is object //
  • 22.
    // test o1={10,'c'}; // //cout<<o1.a<<endl<<o1.c<<endl; // // //} // ---------------- private------------ class test { private: int a; char c; }; int main() { //objects are instance of class // here o1 is object test o1={10,'c'}; cout<<o1.a<<endl<<o1.c<<endl; } // private will give error //error: c and a is private
  • 23.
    2. class_function.cpp /* private: datamember under this access specifier can be accessed only with in the class public: can be ACCESSED outside the class */ #include<iostream> using namespace std; class test { // test is tag name //private: // int a; // char c; // void display() // { // cout<<"inside class"<<endl; // } /* will give error: display is private */
  • 24.
    private: int a; char c; public: //void display() // { // cout<<"inside class"<<endl; // cout<<"a= "<<a<<" c = "<<c<<endl; // } void init(int x, char ch) { a=x; c=ch; } void display() { cout<<"inside class"<<endl; cout<<"a= "<<a<<" c = "<<c<<endl; } // display function can be written below or above the init() function }; int main() { //objects are instance of class // here o1 is object
  • 25.
    test o1; o1.init(10,'A'); o1.display(); /* o1.init(10,'A');// error: will print garbage value because we are displaying before initialing/ assigning value in a and c */ } 3. class_function1.cpp /* private: data member under this access specifier can be accessed only with in the class public: can be ACCESSED outside the class */ #include<iostream> using namespace std; class test { //private: // int a; // char c; int a;
  • 26.
    char c; //bydefault they are private access specifier only public: void init(int x, char ch) { a=x; c=ch; } void display() { cout<<"inside class"<<endl; cout<<"a= "<<a<<" c = "<<c<<endl; } }; int main() { test o1,o2; o1.init(10,'A'); o1.display(); o2.init(20,'B'); o2.display(); /* Each object(o1 and o2 both ) and each variable(a and c both ) will get different memory */
  • 27.
    } /* structure : publicis by default access specifier class: private is by default access specifier */ 4. class_student.cpp /* Problem: create a class make all data member private input and display */ #include<iostream> #include<string.h> using namespace std; class student { private: char name[20]; int id; float marks; public:
  • 28.
    void init(const char*nam, int idn, float mark) { strcpy(name,nam); // strcpy(destination,source); id = idn; marks= mark; } void display() { cout<<"name :"<<name<< " id :"<<id<<" marks :"<<marks<<endl; } }; int main() { student s; char Name[20]; int Id; float Marks; cout<<"Enter student name, id and marks"<<endl; cin>>Name>>Id>>Marks; s.init(Name,Id,Marks); s.display(); return 0;
  • 29.
    } 5. constructor.cpp /* constructor: constructor nameshould having only what is class name a. default constructor: 1.constructor which is not having any argument 2. there is no need of calling default constructor 3. automatically compiler will call the default constructor */ #include<iostream> using namespace std; class test { int a; char name[20]; public: // ------------------- default constructor ---------------- test() { cout<<"Default constructor "<<endl; } void display()
  • 30.
    { cout<<"In display "<<endl; } }; intmain() { test obj; obj.display(); test obj1; // default constructor will get called } 6. param_constructor.cpp /* b. parameterized constructor : constructor which is having arguments or parameters c. copy constructor : when creating a new object from existing object */ #include<iostream> #include<string.h> using namespace std; class test { int a; char name[20]; public:
  • 31.
    // ------------------- defaultconstructor ---------------- test() { cout<<"--------- In Default constructor -----------"<<endl; } // --------------------- parameterized constructor --------- test(int x, const char*s) { cout<<"-------- In parameterized constructor -----------"<<endl; a=x; strcpy(name,s); } // ------------------ copy constructor ------------------- // test (test m) // its recursive call thus need to pass reference as shown below test(test &m) { cout<<"-------- In copy constructor -----------"<<endl; a = m.a; strcpy(name, m.name); } void display() { cout<<"-------- In display function --------------"<<endl; cout<<"a :"<<a<<" name :"<<name<<endl;
  • 32.
    } }; int main() { test obj;// default constructor will get called //test obj(12,"varsity"); // error: redeclaration of obj test obj1(12,"varsity"); // it will call to parameterized constructor obj1.display(); cout<<"------ In main function -----------"<<endl; // test obj2=obj1; test obj2(obj1); // it will call to copy constructor obj2.display(); } 7. student_constructor.cpp #include<iostream> #include<string.h> using namespace std; class student { private: char name[20];
  • 33.
    int id; float marks; public: student(constchar *nam, int idn, float mark) // parameterized constructor { cout<<"--------- In Parameterized constructor -----------"<<endl; strcpy(name,nam); // strcpy(destination,source); id = idn; marks= mark; } student(student &m) // copy constructor { cout<<"--------- In copy constructor -----------"<<endl; strcpy(name,m.name); // strcpy(destination,source); id = m.id; marks= m.marks; } void display() { cout<<"name :"<<name<< " id :"<<id<<" marks :"<<marks<<endl; } }; int main()
  • 34.
    { // student s;// will give error because there is no default constructor char Name[20]; int Id; float Marks; cout<<"Enter student name, id and marks"<<endl; cin>>Name>>Id>>Marks; student s(Name,Id,Marks); // using parameterized constructor s.display(); student s1(s); // using copy constructor s1.display(); return 0; } 8. Defaultcompliler_constructor.cpp /* b. parameterized constructor : constructor which is having arguments or parameters c. copy constructor : when creating a new object from existing object */ #include<iostream> #include<string.h>
  • 35.
    using namespace std; classtest { int a; char name[20]; public: void display() { cout<<"-------- In display function --------------"<<endl; cout<<"a :"<<a<<" name :"<<name<<endl; } }; int main() { test obj(12,"varsity"); // it will call to parameterized constructor obj.display(); test obj2(obj); // it will call to copy constructor obj2.display(); } 9. destructor.cpp /* Destructor: to release the memory of objects destructor will be used when object is going out of scope --- automatically destructor will get call */
  • 36.
    #include<iostream> #include<string.h> using namespace std; classtest { public: test() // constructor { cout<<"Default constructor:n"; } ~test() // destructor { cout<<"Destructor:n"; } }; void fun() { cout<<"In fun:n"; test o1; cout<<"end of fun:n"; } int main() { cout<<"In main calling to fun:n"; fun();
  • 37.
    cout<<"end of main:n"; } 10.parameterized_const&destructor.cpp /* Destructor: to release the memory of objects destructor will be used when object is going out of scope --- automatically destructor will get call */ #include<iostream> #include<string.h> using namespace std; class test { int a; char c; public: test() // constructor { cout<<"Default constructor:n"; } ~test() // destructor { cout<<"in Destructor:n"; } test (int x,char ch) {
  • 38.
    cout<<"In parameterized constructor:n"; x=a; c=ch; cout<<"endof param. const:n"; } void display() { cout<<"----- In display:nn"; // cout<<"a = "<<a<<"c ="<<c<<endl; // cout<<"end of displaynn"; } }; void fun() { { cout<<"In fun:nn"; test o1; // o1 is getting memory in stack segmentation only cout<<"end of fun:nn"; } cout<<"calling to parameterized contructor:nn"; test obj(1,'A'); obj.display(); }
  • 39.
    int main() { cout<<"In maincalling to fun:nn"; fun(); // cout<<"calling to parameterized contructor:n"; // test obj(1,'A'); // obj.display(); cout<<"end of main:nn"; // first disable test.obj() from fun and observe ..... than disable from //main function and observe when written in fun() } /----------------------------------- ------------ february 22, 2018 ---------------------------------------- Dynamic memory allocation: 1. NewOperator.cpp /* Dynamic memory allocation : two operator 1. New : for allocating the memory 2. Delete : for deallocating the memory Pointer = new data type return type of new and delete is *pointer */ #include<iostream> using namespace std; int main()
  • 40.
    { int *p =new int(100); int *pointr = new int; *pointr = 100; cout<<"*pointer :"<<*pointr<<endl; cout<<"Size of int pointer :"<<sizeof(pointr)<<"n"; cout<<"Size of int pointer p = int(100) :"<<sizeof(p)<<"n"; char *ch = new char; *ch = 'A'; cout<<"Size of char pointer :"<<sizeof(ch)<<"n"; delete pointr; delete ch; cout<<"value on *pointer after deletion of pointer :"<<*pointr<<endl; cout<<"after deleting Size of char pointer :"<<sizeof(ch)<<"n"; } 2. newfordynamicARRAY.cpp /* dynamic memory allocation for run time array we are allocating memory in heap segment only */ #include<iostream>
  • 41.
    using namespace std; intmain() { int *p = new int[5]; /* cout<<"Size of empty pointer :"<<sizeof(p)<<"n"; for(int i=0;i<5;i++) { cout<<"*p :"<<*(p+i)<<endl; } */ cout<<"Enter value :"<<endl; // for(int i=0;i<5;i++) // { // cin>>*(p+i); // // } for(int i=0;i<5;i++) { cin>>p[i]; } // for(int i=0;i<5;i++)
  • 42.
    // { // cout<<"*p:"<<*(p+i)<<endl; // } for(int i=0;i<5;i++) { cout<<"*p :"<<*p<<endl; p++; } cout<<"Size of pointer :"<<sizeof(p)<<"n"; // to delete entire array delete [] p; } 3. memoryForObject.cpp /* Memory for object */ #include<iostream> using namespace std; class test { public:
  • 43.
    test() { cout<<"Default constructor :n"; } ~test() { cout<<"Destructor:n"; } }; int main() { /* test *obj = new test; delete obj; */ /* Here obj is pointer type object for class this obj is getting memory in heap segment but in parameterized_const&destructor.cpp o1 is getting memory in stack segment */ test *obj = new test[5]; /* here creating 5 time object thus five time it will call to default constructor and destructor */
  • 44.
    delete [] obj; } 4.malloc.cpp /* difference b/w malloc and new */ #include<iostream> #include<stdlib.h> using namespace std; class test { public: test() { cout<<"Default constructor :n"; } ~test() { cout<<"Destructor :n"; } }; int main() {
  • 45.
    test *ptr =(test*)malloc(sizeof(test)); free(ptr); /* default constructor will not get called */ } 5. parameterized_constUsingDMA.cpp /* because ptr is pointer type object thus instead of . operator use -> operator */ #include<iostream> #include<stdlib.h> using namespace std; class test { int a; public: test(int x) { a=x;
  • 46.
    cout<<"Parameterized constructor :n"; } voiddisplay() { cout<<"a :"<<a<<endl; } ~test() { cout<<"In Destructor :n"; } }; int main() { test *ptr = new test(10); ptr ->display(); delete ptr; } 6. default_paraConstUsingDMA.cpp /* because ptr is pointer type object thus instead of . operator use -> operator */ #include<iostream> #include<stdlib.h> using namespace std; class test
  • 47.
    { int a; public: test(int x=0) { a=x; cout<<"Parameterizedas well as default constructor :n"; } void display() { cout<<"a :"<<a<<endl; } ~test() { cout<<"In Destructor :n"; } }; int main() { test o1; // this will call to default constructor --- test(int x=0) cout<<"IN main :n"; test *ptr = new test(10); // this will call to parameterized constructor---test(int x=0) ptr ->display(); delete ptr; }
  • 48.
    7. shallowCopy.cpp /* shallow copy: whenhaving inbuild copy constructor ,, both object(str1 and str2) will share same memory thus when we comment that part it shows error ;; core dumped i.e segmentation failed */ /* Deepcopy: when we are using user defined constructor each object(str1 and str2) will get different memory */ #include<iostream> #include<cstring> using namespace std; class String { private: char *s; int size; public: String(const char *str = NULL); // constructor
  • 49.
    ~String(){delete []s;} //----- used to release the memory of object String(const String&); // copy constructor void print() {cout<<s<<endl;} // function to print string void change(const char *); // function to change }; String:: String(const char *str) { size = strlen(str); s = new char[size+1]; strcpy(s,str); } void String::change(const char *str) { delete [] s; s= new char[size+1]; strcpy(s,str); } /* String::String(const String& old_str) { size=old_str.size; s = new char[size+1]; strcpy(s,old_str.s);
  • 50.
    } */ int main() { String str1("cranes"); Stringstr2 = str1; str1.print(); // what is printed str2.print(); str1.change("banglore"); str2.change("CranesVarsity"); str1.print(); // what is printed str2.print(); return 0; } ----------------------------------------------- february23, 2018 ----------------------------------- 1. const_datamember.cpp /* initializer list is used to initialize the const as well as non-const variable
  • 51.
    */ #include<iostream> using namespace std; classtest { const int a,b; int c; public: // test(int x=0,int y=0) default constructor as well as parameterized constructor test(int x=0,int y=0):a(x),b(y),c(10) { // a=x,b=y; no need to write like this // initializer list .... non-const variable can be started using any variable } void display() { cout<<a<<" "<<b<<" "<<c<<endl; } }; int main() { test o1(10,20); o1.display(); }
  • 52.
    2. const_memberfunction.cpp /* const memberfunction can access/use all the data member of a class but can not change any of the values. */ #include<iostream> using namespace std; class test { const int a,b; int c; public: test(int x=0,int y=0):a(x),b(y),c(10) { } /* void display() { a=50; // in simple member function data member can be modified cout<<a<<" "<<b<<" "<<c<<endl;
  • 53.
    } */ void display()const { a=50; /* but inthe const member function data member can not modified they are read only object */ cout<<a<<" "<<b<<" "<<c<<endl; } }; int main() { test o1(10,20); o1.display(); } 3. const_object.cpp /* const object can call only const member function
  • 54.
    but a normalobject can call to const member function */ #include<iostream> using namespace std; class test { const int a,b; int c; public: test(int x=0,int y=0):a(x),b(y),c(10) { } // void display() // { // cout<<a<<" "<<b<<" "<<c<<endl; // } void display()const { cout<<a<<" "<<b<<" "<<c<<endl;
  • 55.
    } }; int main() { const testo1(10,20); test o2(30,40); // here o1 is const object o1.display(); o2.display(); } 4. static.cpp /* writing int a; will get memory in data segment ,,,, can be accessed using &a ................................ 1.there is no static keyword in c structure 2.static data member can not be initialized inside the class they can only be initialized outside the class to initialize a static data member outside the class follow the syntax:
  • 56.
    data_type class_name::data_member=value; ................. herestatic data member(a) is not related with object(o1 and o2) ,,, it is only with respect to class .... thus every object(o1 and 02) will get only same/ single memory ..... in previous case both object were having different memory 3. default value of static variable is 0 */ #include<iostream> using namespace std; class test { static int a; public: void display() { cout<<"a :"<<a<<endl; } };
  • 57.
    int test::a=10; int main() { testo1; o1.display(); test o2; o2.display(); } 5. static_memberfunction.cpp #include<iostream> using namespace std; class test { static int a; int b=20; public: static void display() { //cout<<"a :"<<a<<" b :"<<b<<endl; /* static member function can not access non-static data member of a class
  • 58.
    */ cout<<"a :"<<a<<endl; } }; int test::a=10; intmain() { test o1; o1.display(); test::display(); } 6. static_localVariable.cpp /* #include<iostream>
  • 59.
    using namespace std; classtest { int b; public: test() { cout<<"Default constructor n"; } ~test() { cout<<"Destructor n"; } }; void fun() { test o1; } int main() { fun();
  • 60.
    cout<<"IN main n"; } ans: defaultconstructor destructor In main */ #include<iostream> using namespace std; class test { int b; public: test() { cout<<"Default constructor n"; }
  • 61.
    ~test() { cout<<"Destructor n"; } }; void fun() { statictest o1; } int main() { fun(); cout<<"In main n"; } // ----------------------------------------------------------------- /* scope of local static variable (static test o1) is through out the program,,,,
  • 62.
    thus output is ------------------destructor get called scope is ending default constructor In main destructor */ 7. newfor2DArray.cpp #include<iostream> #define row 3 #define col 3 using namespace std; int main() { int (*p)[col] = new int[row][col]; // pointer to an array: always use column size //int *p[col] = new int[row][col]; // Array pointer cout<<"Enter value :"<<endl; for(int i=0;i<row;i++) { for(int j=0;j<col;j++)
  • 63.
    { cin>>p[i][j]; } } cout<<"matrix :n"; for(int i=0;i<row;i++) { for(intj=0;j<col;j++) { cout<<"p[i][j] :"<<p[i][j]<<endl; } cout<<endl; } delete []p; } ---------------------------------------- February 26,2018 ----------------------------------------------------------------- 1. friend.cpp /* Friends: to access the private data member outside the class. friend is a keyword which is used to access private or protected data member outside the class of non-member function.
  • 64.
    */ #include<iostream> using namespace std; classtest { int a,b; public: test(int a=0,int b=0) // default as well as parameterized constructor { this ->a=a; this ->b=b; } // void multi(int a, int b); friend void multi(test); }; //void test:: multi(int x,int y) //{ // //} void multi(test m) { int c; c = m.a*m.b; cout<<"c :"<<c<<endl; }
  • 65.
    int main() { test o1(10,20); multi(o1); } 2.friend_second.cpp #include<iostream> using namespace std; class test { int meter; public: test(int a=0) { meter = a; } friend int addfive(test); }; int addfive(test m) {
  • 66.
    m.meter = 5; returnm.meter; } int main() { test o1; cout<<"meter :"<<addfive(o1)<<endl; } 3. friend_passbyvalue.cpp #include<iostream> using namespace std; class test { int meter; public: test(int a=0) { meter = a; } friend int addfive(test); void display()
  • 67.
    { cout<<"nmeter in class:"<<meter; } }; int addfive(test m) { m.meter = 5; return m.meter; } int main() { test o1; cout<<"meter in main :"<<addfive(o1)<<endl; o1.display(); } 4. friend_passbyReference.cpp #include<iostream> using namespace std; class test { int meter;
  • 68.
    public: test(int a=0) { meter =a; } friend int addfive(test&); void display() { cout<<"nmeter in class :"<<meter; } }; int addfive(test& m) { m.meter = 5; return m.meter; } int main() { test o1; cout<<"meter in main :"<<addfive(o1)<<endl; o1.display(); } 5. friend_InClass.cpp
  • 69.
    /* class friendship isonly one way direction .... as radius from class A can be accessed in class B but area from class B can not be accessed in class A */ #include<iostream> using namespace std; class A { int radius; public: A() { radius = 10; } void showA() { cout<<"nradius :"<<radius; } friend class B; }; class B {
  • 70.
    int area; public: void find_area(Am) { area = m.radius*m.radius; cout<<"narea :"<<area; } }; int main() { A oA; B oB; oA.showA(); oB.find_area(oA); } 6. friend_calculator.cpp #include<iostream> using namespace std; class cal { double a, b;
  • 71.
    public: cal(double a,double b) { this->a=a; this ->b=b; } friend double add(cal); friend double sub(cal); friend double mul(cal); friend double div(cal); }; double add(cal m) { return (m.a+m.b); } double sub(cal m) { if(m.a>m.b) return (m.a - m.b); else return (m.b - m.a); } double mul(cal m) {
  • 72.
    return (m.a *m.b); } double div(cal m) { return (m.b/m.a); } int main() { cal ALU(20,10); cout<<"Add :"<<add(ALU)<<endl; cout<<"Sub :"<<sub(ALU)<<endl; cout<<"Mul :"<<mul(ALU)<<endl; cout<<"div :"<<div(ALU)<<endl; } 7. Opeartor_Overloading_Pre_increment.cpp /* Operator overloading: extending operation on object Allows c++ operators to be given new definition so that they can be used with user defined data types as with built in data type. To write operator overloaded function we either use member functions or friend function syntax:
  • 73.
    return_type operator #(arguments) { //code } UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */ #include<iostream> using namespace std; class ope { int a; public: ope(int x=0) { a=x; } void display() { cout<<"a :"<<a<<endl;
  • 74.
    } /* ++ is anunary operator and we are calling it for member function of class so number of argument's are zero */ void operator ++() { // automatically compiler is calling this function when we are writing ++o ++a; } }; int main() { ope o(10); o.display(); ++o; o.display(); } 8. Opeartor_Overloading_for_friend.cpp /*
  • 75.
    syntax: return_type operator #(arguments) { //code } UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */ #include<iostream> using namespace std; class ope { int a; public: ope(int x=0) { a=x;
  • 76.
    } void display() { cout<<"a :"<<a<<endl; } /* ++is an unary operator and we are calling it for friend function of class so number of argument's are one */ friend void operator ++(ope&); }; void operator ++(ope& m) { ++m.a; } int main() { ope o(10); o.display(); ++o; o.display(); }
  • 77.
    9. operator_overloading_Post_increment.cpp #include<iostream> using namespacestd; class ope { int a; public: ope(int x=0) { a=x; } void display() { cout<<"a :"<<a<<endl; } /* In order to differentiate b/w pre-increment and post increment we need to use a dummy variable.... because compiler is not getting whether ++ (IN void operator ++()) is for pre- increment or for post
  • 78.
    increment . */ void operator++(int f) { a++; } }; int main() { ope o(10); o.display(); o++; o.display(); } 10. operator_overloading_Post_increment_friend.cpp /* syntax:
  • 79.
    return_type operator #(arguments) { //code } UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */ #include<iostream> using namespace std; class ope { int a; public: ope(int x=0) { a=x; } void display() {
  • 80.
    cout<<"a :"<<a<<endl; } /* ++ isan unary operator and we are calling it for friend function of class so number of argument's is one,,, second argument is dummy variable */ // friend void operator ++(int f, ope&); friend void operator ++(ope&, int f); /* dummy variable must be taken as second argument */ }; //void operator ++(int x, ope& m) void operator ++(ope& m, int x) { m.a++; } int main() { ope o(10); o.display();
  • 81.
    o++; o.display(); } 11. Binary_Operator_overloading.cpp /* UNARY BINARY member0 argument 1 argument friend 1 argument 2 argument o1 o2 a=10 a=100 b= 20 b=200 */ #include<iostream> using namespace std; class bin { int a,b; public:
  • 82.
    bin(int x=0, inty=0) { a=x,b=y; } void display() { cout<<"a :"<<a<<" b :"<<b<<endl; } /* member function , binary operator --- thus no.of argument = 1 */ void operator +(bin m) { /* bin m is directly pointing to the second object(o2) ,,,, thus m.a means o2.a and m.b means o2.b writing only a and b will make internal link with first object... thus writing a means o1.a and writing b means o2.b */ bin o3; o3.a = a+m.a; o3.b = b+m.b;
  • 83.
    cout<<"na :"<<o3.a<<" b:"<<o3.b<<endl; } }; int main() { bin o1(10,20); bin o2(100,200); o2.display(); o1.display(); o1+o2; } 12. Return_Binary_Operator_overloading.cpp /* UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */
  • 84.
    #include<iostream> using namespace std; classbin { int a,b; public: bin(int x=0, int y=0) { a=x,b=y; } void display() { cout<<"a :"<<a<<" b :"<<b<<endl; } /* member function , binary operator --- thus no.of argument = 1 */ bin operator +(bin m) { bin o3; o3.a = a+m.a;
  • 85.
    o3.b = b+m.b; returno3; } }; int main() { bin o1(10,20); bin o2(100,200); o2.display(); o1.display(); bin o4 = o1+o2; o4.display(); } 13. Binary_Operator_overloading_friend.cpp /* UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument
  • 86.
    o1 o2 a=10 a=100 b=20 b=200 */ #include<iostream> using namespace std; class bin { int a,b; public: bin(int x=0, int y=0) { a=x,b=y; } void display() { cout<<"a :"<<a<<" b :"<<b<<endl; } /* friend function , binary operator --- thus no.of argument = 2 */
  • 87.
    friend bin operator+(bin ,bin); }; bin operator +(bin m1, bin m2) { bin o3; o3.a = m1.a+m2.a; // m1.a means o1.a and m2.a means o2.a o3.b = m1.b+m2.b; return o3; } int main() { bin o1(10,20); bin o2(100,200); o2.display(); o1.display(); bin o4 = o1+o2; o4.display(); } ----------------------------------- jbu ---------------------------------------------------------------------------------
  • 88.
    ------------------------------- february27, 2018----------------------------------------------------------- 1. Bitwisw_NotOperator_overloading.cpp /* UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */ #include<iostream> using namespace std; class bin { int a; public: bin(int x=0) { a=x; } void display() {
  • 89.
    cout<<"a :"<<a<<endl; } /* member function, unary operator --- thus no.of argument = 0 */ // void operator ~() // { // a = ~a; // // } /* friend function, unary operator --- thus no. of argument = 1 */ friend void operator ~(bin&); }; void operator ~(bin& m) { m.a = ~m.a; } int main()
  • 90.
    { bin o1(16); /* because 1'scompliment of 16 = 01111 and 2's compliment of 01111 = 10001 which is equal to 17 and 17 = -17 for - sign go through signed and unsigned modifier. */ o1.display(); ~o1; o1.display(); } 2. Bitwise_OR_operator_overloading.cpp /* UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */
  • 91.
    #include<iostream> using namespace std; classbitWiseOR { int a; public: bitWiseOR(int x=0) { a=x; } void display() { cout<<"a :"<<a<<endl; } /* member function , binary operator --- thus no.of argument = 1 // member function ----- bitWiseOR operator |(bitWiseOR m) { bitWiseOR o3;
  • 92.
    o3.a = a|m.a; returno3; } */ /* friend function , binary operator --- thus no.of argument = 2 */ friend bitWiseOR operator |(bitWiseOR& ,bitWiseOR&); }; bitWiseOR operator |(bitWiseOR& m1, bitWiseOR& m2) { bitWiseOR o3; o3.a = m1.a|m2.a; return o3; } int main() { bitWiseOR o1(10); bitWiseOR o2(6); o1.display();
  • 93.
    o2.display(); bitWiseOR o4 =o1|o2; o4.display(); } 3. Real&IMG_overloading.cpp /* UNARY BINARY member 0 argument 1 argument friend 1 argument 2 argument */ #include<iostream> using namespace std; class bin { int real,img; public: bin(int x=0, int y=0) {
  • 94.
    real=x,img=y; } void display() { cout<<"A+jB :"<<real<<"+ j"<<img<<endl; } /* member function , binary operator --- thus no.of argument = 1 */ bin operator +(bin m) { bin o3; o3.real = real + m.real; o3.img = img + m.img; return o3; } }; int main() {
  • 95.
    bin o1(5,10); bin o2(10,20); bino4 = o1+o2; o4.display(); } 4. string.cpp /* string : is a predefined class which is used to declare variable as string */ #include<iostream> #include<cstring> using namespace std; int main() { string s1; string s2; cout<<"nEnter two stringn"; cin>>s1; cin>>s2;
  • 96.
    //int len =strlen(s1); /* we can not do this because s1 not a variable its object */ int len = s1.size(); int len1 = s2.size(); cout<<"length of s1 :"<<len<<endl; cout<<"Length of s2 :"<<len1<<endl; /* s2 is an object */ } 5. String_function.cpp #include<iostream> #include<cstring> using namespace std; int main() { string s1; string s2; cout<<"nEnter two stringn";
  • 97.
    cin>>s1; cin>>s2; string s3; //string copy s3=s1; cout<<"s3:"<<s3<<endl; //string concatenation s3=s1+"_"+s2; cout<<"s3 :"<<s3<<endl; } 6. String_comparison.cpp /* if s1 === s2 ,result = 0 if s1>s2 , result = 1 if s1<s2 , result = -1 */
  • 98.
    #include<iostream> #include<cstring> using namespace std; intmain() { string s1; string s2; cout<<"Enter two string :n"; cin>>s1>>s2; int res = s1.compare(s2); /* compare by ascii value thus cranes is greater than Cranes case ignoring method is not their is c++ */ if(res == 0) cout<<"string have same datan"; else if(res == 1) cout<<"s1 = "<<s1<< " is greatern"; else cout<<"s2 = "<<s2<< " is greatern"; } 7. My_string_Overload.cpp : assignment question to perform overloading on string function
  • 99.
    #include<iostream> #include<cstring> using namespace std; classMy_string { char s1[20],s2[20]; public: My_string(const char *x=NULL, const char *y=NULL) { strcpy(s1,x); strcpy(s2,y); } friend void operator +(My_string , My_string ); friend void operator ==(My_string, My_string); //friend void operator !=(My_string, My_string); //friend void opeartor =(My_string,My_string); void operator =(My_string m1) { strcpy(s1, m1.s1); strcpy(s2, m1.s2);
  • 100.
    cout<<"m1.s1 :"<<m1.s1<<"t"<<"m1.s2 :"<<m1.s2<<endl; } }; //voidoperator =(My_string m1, My_string m2) //{ // strcpy(m2.s1, m1.s1); // strcpy(m2.s2, m1.s2); // // cout<<"m1.s1 :"<<m1.s1<<"t"<<"m1.s2 :"<<m1.s2<<endl; // cout<<"m2.s1 :"<<m2.s1<<"t"<<"m2.s2 :"<<m2.s2<<endl; // // //} //void operator !=(My_string m1, My_string m2) //{ // //int res = (m1.s1).compare(m2.s1); // /* // can not perform because s1 and s2 are variable not the object // */ // // int res = strcmp(m1.s1,m2.s1); // if(res == 0)
  • 101.
    // cout<<"string havesame datan"; // else if(res == 1) // cout<<"m1.s1 = "<<m1.s1<< " is greatern"; // else // cout<<"m2.s1 = "<<m2.s1<< " is greatern"; // // // ------------------------------------------------------ // // int res1 = strcmp(m1.s2,m2.s2); // if(res1 == 0) // cout<<"string have same datan"; // else if(res1 == 1) // cout<<"m1.s2 = "<<m1.s2<< " is greatern"; // else // cout<<"m2.s2 = "<<m2.s2<< " is greatern"; // //} // // // void operator ==(My_string m1, My_string m2) { //int res = (m1.s1).compare(m2.s1); /* can not perform because s1 and s2 are variable not the object
  • 102.
    */ int res =strcmp(m1.s1,m2.s1); if(res == 0) cout<<"string have same datan"; else if(res == 1) cout<<"m1.s1 = "<<m1.s1<< " is greatern"; else cout<<"m2.s1 = "<<m2.s1<< " is greatern"; // ------------------------------------------------------ int res1 = strcmp(m1.s2,m2.s2); if(res1 == 0) cout<<"string have same datan"; else if(res1 == 1) cout<<"m1.s2 = "<<m1.s2<< " is greatern"; else cout<<"m2.s2 = "<<m2.s2<< " is greatern"; } void operator +(My_string m1, My_string m2) { char ch1[30],ch2[30]; strcpy(ch1,m1.s1); strcat(ch1,m2.s1);
  • 103.
    strcpy(ch2,m1.s2); strcat(ch2,m2.s2); cout<<"ch1 :"<<ch1<<" ch2:"<<ch2<<endl; } int main() { My_string obj1("Cranes","banglore"); My_string obj2("varsity","banglore"); obj1 + obj2; // string cat obj1 == obj2; // string cmp //obj1 != obj2; obj1 = obj2; // assigning one string into another /* we can overload any operator except the 1:sizeof operator, 2:typedef operator 3: dot(.) operator , 4: conditional operator(?, :):because we can use max two operator but in ternary or conditional operator we need three operator
  • 104.
    5: (.*) operator */ } 8.read_space_In_string.cpp #include<iostream> #include<cstring> using namespace std; int main() { string s1; getline(cin,s1); cout<<s1<<endl; } ------------------------------------------------ february28, 2018 ------------------------------------------------------------ 1. Inheritance.cpp /* Inheritance: Mechanism of reusing and extending existing classes without modifying them. 1. newly created classes ----> derived classes/sub classes/ child classes
  • 105.
    2. existing class-----> base class/super class/parent class -------------------------------------------- why inheritance: 1.re usability is main advantages 2. easy debugging 3. extandibility -------------------------------------------- private attributes of class are inherited but inaccessible in inherited class protected attributes of class are inherited as well as accessible in inherited class --------------------------------------------- syntax: class Base_class_name { --- }; class derived_class_name: mode Base_class_name { --- }; when we call derived class object it will automatically call to base class
  • 106.
    -------------- A derived classinherits all base class functions with the following exceptions: 1. constructor, destructor and copy constructor of the base class 2. overloaded operators of the base class 3. the friend function of the base class ---------------------------------- mode: 1.public 2.private 3.protected ********************* private data member can not be accessed in derived class only public and protected data members can be accessed ---------------------------------- Types of inheritance: 1. Single Inheritance 2. Multilevel Inheritance 3. Hierarchical inheritance: see diagram from note book 4. Multiple Inheritance 5. Hybrid/Diamond inheritance
  • 107.
    ************* single inheritance: onlyone class can be derived from base class class base { }; class derived : mode base { }; ************* Multilevel Inheritance: BASE <----- derived1 <------ derived2 derived1 is inheriting from base class,,,, and derived2 is inheriting from derived1 ,,,, means derived2 class have data member of both base class as well as derived1 class class base { }; class derived1: mode base
  • 108.
    { }; class derived2: modebase { }; */ 2. Inheritance_demo.cpp #include<iostream> using namespace std; class base { public: base() //3. { cout<<"In base default constructorn"; } ~base() //7. { cout<<"In base destructorn"; } };
  • 109.
    class derived: publicbase { public: derived() //2. it will arrive here but before printing this it will goto base default const. { //4. // in case of constructor ,,, base class const.will get call first and then // derived class constructor will get call. cout<<"In derived default constructorn"; } ~derived() //6. { // in case of destructor ,,,, derived class destructor will get call first // and then the base class destructor will get call cout<<"In derived destructorn"; } }; int main() { //1. cout<<"in main calling default constructor using object of derived classn";
  • 110.
    derived obj; cout<<"In mainn";//5. } 3. Inheriting_dataMemberFrom_baseTOderivedClass.cpp #include<iostream> #include<cstring> using namespace std; class student { protected: char name[20]; float fees; int id; public: student() { cout<<"in base constructorn"; } ~student() { cout<<"In base destructorn"; }
  • 111.
    }; class eng_student:public student { charcollg_name[20]; public: eng_student(const char* clg, const char* nam, float f, int i) { strcpy(collg_name,clg); strcpy(name,nam); fees = f; id = i; } void display() { cout<<"----- In display -----n"; cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl; } ~eng_student() { cout<<"in derived destructorn"; } };
  • 112.
    int main() { cout<<"inside main,,,creating object for derived classn"; eng_student obj("techno","Narayan", 45000, 1); cout<<"in main ,,, calling displayn"; obj.display(); } 4. Inheritance_Initializing_parameterized_constructorofBASEclass.cpp /* In inheritance ,,, we can not call directly parameterized constructor of base class in main thus need to use list */ #include<iostream> #include<cstring> using namespace std;
  • 113.
    class student { protected: char name[20]; floatfees; int id; public: student(const char* nam, float f, int i) { cout<<"in base constructorn"; strcpy(name,nam); fees = f; id = i; } ~student() { cout<<"In base destructorn"; } }; class eng_student:public student { char collg_name[20]; public:
  • 114.
    // eng_student(const char*clg) // { // strcpy(collg_name,clg); // // } eng_student(const char* clg):student("varun",45000,2) { strcpy(collg_name,clg); cout<<"In derived constructorn"; } void display() { cout<<"----- In display -----n"; cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl; } ~eng_student() { cout<<"in derived destructorn"; } }; int main() {
  • 115.
    cout<<"in mainn"; // studentobj("Narayan", 45000, 1); eng_student obj("techno"); cout<<"in main ,,, calling displayn"; obj.display(); } 5. Inheritance_Initializing_parameterized_constructorofBASEclass_1.cpp #include<iostream> #include<cstring> using namespace std; class student { protected: char name[20]; float fees; int id; public: student(const char* nam, float f, int i) {
  • 116.
    cout<<"nnin base constructorinitializing the data membern"; strcpy(name,nam); fees = f; id = i; } ~student() { cout<<"In base destructorn"; } }; class eng_student:public student { char collg_name[20]; public: eng_student(const char* clg,const char* nam, float f, int i):student(nam,f,i) { cout<<"In derived constructor,, copying clg namen"; strcpy(collg_name,clg); } void display() { cout<<"----- In display -----n";
  • 117.
    cout<<collg_name<<" "<<name<<" "<<fees<<""<<id<<endl; } ~eng_student() { cout<<"in derived destructorn"; } }; int main() { cout<<"in mainn"; eng_student obj1("techno","Narayan",45000,1); cout<<"in main ,,, calling display for obj1n"; obj1.display(); eng_student obj2("techno","varun",45000,2); cout<<"in main ,,, calling display for obj2n"; obj2.display(); /* destructor will get call twice when obj1 and obj2 will go out of scope */ } 6. Inheritance_Copy_constructor.cpp
  • 118.
    #include<iostream> #include<cstring> using namespace std; classstudent { protected: char name[20]; float fees; int id; public: student() { } student(const char* nam, float f, int i) { cout<<"nnin base constructor initializing the data membern"; strcpy(name,nam); fees = f; id = i; } ~student() { cout<<"In base destructorn";
  • 119.
    } }; class eng_student:public student { charcollg_name[20]; public: eng_student() { } eng_student(const char* clg,const char* nam, float f, int i):student(nam,f,i) { cout<<"In derived constructor,, copying clg namen"; strcpy(collg_name,clg); } eng_student(eng_student& m) { cout<<"nnIn copy constructor n"; strcpy(collg_name, m.collg_name); strcpy(name, m.name); fees = m.fees; id = m.id; }
  • 120.
    void display() { cout<<"----- Indisplay -----n"; cout<<collg_name<<" "<<name<<" "<<fees<<" "<<id<<endl; } ~eng_student() { cout<<"nn************** In derived destructor *************n"; } }; int main() { cout<<"in mainn"; eng_student obj1("techno","Narayan",45000,1); cout<<"in main ,,, calling display for obj1n"; obj1.display(); eng_student obj2=obj1; cout<<"in main ,,, calling display for obj2n"; obj2.display(); /* destructor will get call twice when obj1 and obj2 will go out of scope */
  • 121.
    } 7. Inheritance_fruit.cpp #include<iostream> #include<cstring> using namespacestd; class fruit { protected: int siz; char color[20]; public: fruit() { } fruit(int sizee, const char *clr) { cout<<"nnIn base constructor initializing size and colorn"; siz = sizee; strcpy(color,clr); } ~fruit()
  • 122.
    { cout<<"In base destructorn"; } }; classApple:public fruit { float cost; public: Apple() { } Apple(float cst,int sizz, const char *clrr):fruit(sizz,clrr) { cout<<"in derived constructor ,,, initializing costn"; cost = cst; } Apple(Apple& m) { cout<<"In copy constructornnn"; cost = m.cost; siz = m.siz; strcpy(color, m.color); } ~Apple()
  • 123.
    { cout<<"in derived destructorn"; } voiddisplay() { cout<<"In displayn"; cout<<siz<<" "<<color<<" "<<cost<<endl; } }; int main() { cout<<"in mainn"; Apple obj1(50,2,"red"); cout<<"calling display for obj1n"; obj1.display(); Apple obj2 = obj1; cout<<"calling display for obj2n"; obj2.display(); }
  • 124.
    --------------------- March1, 2018-its the occassion of holy ------------------------------------- 1. Multilevel_Inheritance.cpp #include<iostream> #include<cstring> using namespace std; class student { protected: char name[20]; int id; public: student() { } Student(name,id) Eng_student(project) Comp_student(branch , marks)
  • 125.
    student(const char* nam,int i) { cout<<"nnin base constructor ,, initializing name and id of studentn"; strcpy(name,nam); id = i; } ~student() { cout<<"In base class destructorn"; } }; class eng_student:public student { protected: char project[20]; public: eng_student() { } eng_student(const char *nam, int i, const char* proj):student(nam,i) { cout<<"In derived1 initializing project namen"; strcpy(project,proj);
  • 126.
    } ~eng_student() { cout<<"In derived1 destructorn"; } voiddisplay1() { cout<<name<<" "<<id<<" "<<project<<endl; } }; class comp_student:public eng_student { char brachname[20]; float marks; public: comp_student() { } comp_student(const char* nam, int i, const char* proj, const char* brname,float mrks):eng_student(nam,i,proj) { cout<<"In derived2 constructor , initialing branch name and marksn"; strcpy(brachname,brname); marks = mrks;
  • 127.
    } comp_student(comp_student& m) { cout<<"In derived2copy constructorn"; strcpy(name,m.name); id = m.id; strcpy(project, m.project); strcpy(brachname, m.brachname); marks = m.marks; } void display() { cout<<"*************** In display function ******************n"; cout<<"-- name id project branchname marks n"; cout<<"--"<<name <<" "<<id<<" "<<project<<" "<<brachname<<" "<<marks<<endl; } ~comp_student() { cout<<"nnIn derived2 destructorn"; } }; int main() { cout<<"In main,, creating object for derived1 class n"; eng_student obj1("Narayan",1, "C++");
  • 128.
    obj1.display1(); cout<<"************************************************n"; cout<<"in main,, creatingobject for derived2 classn"; comp_student obj2("Narayan",1,"C++", "ECE",80); cout<<"In main , calling display functionn"; obj2.display(); cout<<"********** in main ,,, creating copy constructor fro obj2n"; comp_student obj3 = obj2; obj3.display(); } 2. Hierarchical_inheritance.cpp student Eng_student Med_student Ece_student Cs_student Ayurvedic Homeopathic
  • 129.
    #include<iostream> #include<cstring> using namespace std; class student { protected: charname[20]; int id; public: student() { } student(const char* nam, int i) { cout<<"nnIn base constructor initializing name and idn"; strcpy(name,nam); id = i; } ~student() { cout<<"In base destructorn"; }
  • 130.
    }; class eng_student:public student { protected: charclgname[30]; public: eng_student() { } eng_student(const char* clg, const char* nam,int i):student(nam,i) { cout<<"In eng_student initializing clg_namen"; strcpy(clgname, clg); } ~eng_student() { cout<<"In eng_student destructorn"; } }; class ece_engg:public eng_student {
  • 131.
    char ece_enggcourse[20]; public: ece_engg() { } ece_engg(const char*ece_course,const char* clg, const char* nam,int i):eng_student(clg,nam,i) { cout<<"IN ece_eng initializing coursen"; strcpy(ece_enggcourse,ece_course); } void display_ece() { cout<<"*************** in ece_eng display************n"; cout<<name<<" "<<id<<" "<<clgname<<" "<<ece_enggcourse<<endl; } ~ece_engg() { cout<<"in ece_engg destructorn"; } }; class comp_engg: public eng_student { char cs_course[20]; public:
  • 132.
    comp_engg() { } comp_engg(const char* cs_cours,constchar* clg, const char* nam,int i):eng_student(clg,nam,i) { cout<<"in comp_eng constructor initializing cs_coursen"; strcpy(cs_course,cs_cours); } void display_comp() { cout<<"****************In cs displayn"; cout<<name<<" "<<id<<" "<<clgname<<" "<<cs_course<<endl; } ~comp_engg() { cout<<"in comp_eng destructorn"; } }; class med_student:public student { protected: char med_clg[30];
  • 133.
    public: med_student() { } med_student(const char* clg,const char* nam,int i):student(nam,i) { cout<<"nnnin med_student initializing med_clg namen"; strcpy(med_clg,clg); } ~med_student() { cout<<"in med_student destructorn"; } }; class Ayurvedic:public med_student { char ayr_course[20]; public: Ayurvedic() { } Ayurvedic(const char* crs, const char*clg, const char* nam, int i):med_student(clg,nam,i) { cout<<"in ayurvedic constructor initilaizing course namen";
  • 134.
    strcpy(ayr_course,crs); } void display_ayurved() { cout<<"nnnn**************in ayurvedicdisplayn"; cout<<name<<" "<<id<<" "<<med_clg<<" "<<ayr_course<<endl; } ~Ayurvedic() { cout<<"in ayurvedic destructorn"; } }; class Homeopathic:public med_student { char hom_course[20]; public: Homeopathic() { } Homeopathic(const char* crs, const char*clg, const char* nam, int i):med_student(clg,nam,i) { cout<<"nnIn homeopathic constructor initializing coursen"; strcpy(hom_course,crs); } void display_hom()
  • 135.
    { cout<<"nnnn**************in homeopath displayn"; cout<<name<<""<<id<<" "<<med_clg<<" "<<hom_course<<endl; } ~Homeopathic() { cout<<"In homeopathic destructorn"; } }; int main() { cout<<"nnnn*****************in main,,, creating object for ece_studentn"; ece_engg obj1("digital_ele","techno","narayan",1); cout<<"nn in main calling display for ece_studentn"; obj1.display_ece(); cout<<"nnnn*****************in main,,, creating object for cs_studentn"; comp_engg obj2("networking","techno","vansh",2); cout<<"nn in main calling display for cs_studentn"; obj2.display_comp();
  • 136.
    cout<<"nnnn*****************in main,,, creatingobject for ayurved_studentn"; Ayurvedic obj3("ayurved","ayurvedic_clc","ramesh",3); cout<<"nn in main calling display for ayurvedic_studentn"; obj3.display_ayurved(); cout<<"nnnn*****************in main,,, creating object for homeopath_studentn"; Homeopathic obj4("homeo","homepathic_clg","mahesh",4); cout<<"nn in main calling display for homeopathic_studentn"; obj4.display_hom(); } 3. Multiple_inheritance.cpp #include<iostream> #include<cstring> using namespace std; Ece_engg Cs_engg embedded
  • 137.
    class ece_engg { protected: char ec_branch[20]; charec_project[20]; public: ece_engg(const char* brch, const char* proj) { cout<<"nnnIn ece_engg constructor initializing branch and projectn"; strcpy(ec_branch,brch); strcpy(ec_project,proj); } ~ece_engg() { cout<<"in ece_engg destructorn"; } }; class cs_engg { protected: char cs_branch[20]; char cs_project[20]; public: cs_engg(const char *brch, const char* proj)
  • 138.
    { cout<<"nnn in cs_enggconstructor initializing branch and projectn"; strcpy(cs_branch,brch); strcpy(cs_project,proj); } ~cs_engg() { cout<<"in cs_engg destructorn"; } }; class embedded:public ece_engg, public cs_engg { char subject[20]; float marks; public: embedded(const char* sub, float mrks,const char* proj):ece_engg("ECE",proj),cs_engg("CSE",proj) { cout<<"in embedded constructor ,initializing subject and marksn"; strcpy(subject,sub); marks = mrks; } void display_cs() { cout<<"nn***************** in cs displayn";
  • 139.
    cout<<cs_branch<<" "<<cs_project<<" "<<subject<<""<<marks<<endl; } void display_ece() { cout<<"nn***************** in ece displayn"; cout<<ec_branch<<" "<<ec_project<<" "<<subject<<" "<<marks<<endl; } ~embedded() { cout<<"nnin embedded destructorn"; } }; int main() { cout<<"In main creating onject for embeddedn"; embedded obj("microprocessor",82, "c++"); obj.display_cs(); obj.display_ece(); }
  • 140.
    4. virtual_in_inheritance.cpp /* ********************************** order ofexecution is from left to right use of virtual keyword can change order of execution ********************************** class base1 {}; class base2 {}; class der: public base1, public base2 {}; ---------------------- order of execution : base1 constructor, base2 constructor,der constructor der destructor, base2 destructor, base1 destructor ---------------- class der: public base2, public base1 {}; ---------------------- order of execution : base2 constructor, base1 constructor,der constructor
  • 141.
    der destructor, base1destructor, base2 destructor ---------------- class der: public base1, virtual public base2 {}; ---------------------- order of execution : base2 constructor, base1 constructor,der constructor der destructor, base1 destructor, base2 destructor ---------------- class der:virtual public base1, public base2 {}; ---------------------- order of execution : base1 constructor, base2 constructor,der constructor der destructor, base2 destructor, base1 destructor ---------------- class der: virtual public base1, virtual public base2 {}; ---------------------- order of execution : base1 constructor, base2 constructor,der constructor der destructor, base2 destructor, base1 destructor
  • 142.
    ---------------- if..... class der:base1, virtualbase2, base3 {}; order of execution: base2 constructor, base1 constructor,base3 constructor, der constructor --- der destructor, base3 destructor, base1 destructor, base2 constructor */ #include<iostream> using namespace std; class base1 { public: base1() { cout<<"base1 default constructorn"; } ~base1() { cout<<"base1 destructorn";
  • 143.
    } }; class base2 { public: base2() { cout<<"base2 defaultconstructorn"; } ~base2() { cout<<"base2 destructorn"; } }; //class der:public base1,public base2 //class der:public base1,public base2 class der:public base1,virtual public base2 //class der:virtual public base1,public base2 //class der:virtual public base1,virtual public base2 //class der:public base1,virtual public base2 { public: der()
  • 144.
    { cout<<"derived default constructorn"; } ~der() { cout<<"deriveddestructorn"; } }; int main() { der d; } 5. deathOfDiamond_OR_VirtualBaseClassConcept.cpp /* final derived class have two copies of base class ,,, thus compiler is ambiguous this is known as death of diamond ----- use of virtual keyword will avoid this problem ,,, as it allows only single copy of base class in derived class */ #include<iostream> using namespace std; class person
  • 145.
    { public: void show() { cout<<"inside personn"; } }; //classmen:public person class men:virtual public person { }; //class women:public person class women:virtual public person { }; class child:public men,public women { }; int main() { child c; c.show(); }
  • 146.
    6. virtual_ASSignmentPage88.cpp #include<iostream> #include<cstring> using namespacestd; class student { protected: char name[10]; int id; public: student() { } // student() // { // strcpy(name,"narayan"); // id = 1; // } student(const char* nam, int i) { cout<<"nnIn base class student constructorn"; strcpy(name,nam); id = i; }
  • 147.
    ~student() { cout<<"in base studentdestructorn"; } }; class ece_engg:virtual public student { protected: char ec_branch[20]; char ec_project[20]; public: ece_engg(const char* brch, const char* proj) { cout<<"nnnIn ece_engg constructor initializing branch and projectn"; strcpy(ec_branch,brch); strcpy(ec_project,proj); } ~ece_engg() { cout<<"in ece_engg destructorn"; } }; class cs_engg:virtual public student { protected:
  • 148.
    char cs_branch[20]; char cs_project[20]; public: cs_engg(constchar *brch, const char* proj) { cout<<"nnn in cs_engg constructor initializing branch and projectn"; strcpy(cs_branch,brch); strcpy(cs_project,proj); } ~cs_engg() { cout<<"in cs_engg destructorn"; } }; class embedded:public ece_engg, public cs_engg { char subject[20]; float marks; public: embedded(const char* sub, float mrks,const char* proj,const char* nam,int i): ece_engg("ECE",proj), cs_engg("CSE",proj), student(nam,i) { cout<<"in embedded constructor ,initializing subject and marksn"; strcpy(subject,sub); marks = mrks;
  • 149.
    } void display_cs() { cout<<"nn***************** incs displayn"; cout<<name<<" "<<id<<" "<<cs_branch<<" "<<cs_project<<" "<<subject<<" "<<marks<<endl; } void display_ece() { cout<<"nn***************** in ece displayn"; cout<<name<<" "<<id<<" "<<ec_branch<<" "<<ec_project<<" "<<subject<<" "<<marks<<endl; } ~embedded() { cout<<"nnin embedded destructorn"; } }; int main() { cout<<"In main creating onject for embeddedn"; embedded obj("microprocessor",82, "c++","Narayan",1); obj.display_ece(); obj.display_cs(); }
  • 150.
    -------------------------------------------------- March 2,2018 -------------------------------------------------------------- 1.functionOverriding.cpp 2.UpCasting.cpp 3.DownCasting.cpp 1. functionOverriding.cpp /* The facility of writing functions with same name,arguments, and return type in both the base class and derived class. */ #include<iostream> using namespace std; class base { int a; public: base() { a=10; } void show() { cout<<"In base t"<<"a :"<<a<<endl; }
  • 151.
    }; class der:public base { intb; public: der() { b=20; } void show() { cout<<"In derived t"<<"b :"<<b<<endl; } }; int main() { base b; b.show(); der d; d.show(); }
  • 152.
    2. UpCasting.cpp /* Upcasting: baseclass pointer pointing to derived class object is technically termed as Upcasting. base_ptr = &base_obj; base_ptr = &der1_obj; base_ptr = &der2_obj; polymorphism: single interface, multiple implementation(example show(): same name but used in both base class and derived class ) compile time polymorphism(function will be called during compile time): function overloading , operator overloading runtime polymorphism: virtual function, late binding late binding : when using virtual keyword b->show() will call to derived class show() function during runtime ,,, this is called late binding BASE Derived1 Derived2
  • 153.
    early binding: writingb->print() will call to base class function ,, during compile time known as early binding... */ #include<iostream> using namespace std; class base { int a; public: base() { a=10; } void print() { cout<<"In print:: Basen"; } // void show() // { // cout<<"In base t"<<"a :"<<a<<endl; // } virtual void show() {
  • 154.
    cout<<"In base t"<<"a:"<<a<<endl; } }; class der:public base { int b; public: der() { b=20; } void show() { cout<<"In derived t"<<"b :"<<b<<endl; } void print() { cout<<"In print::derivedn"; } }; int main() { //base b.show(); not allowed base *b; der d;
  • 155.
    b = &d;// Upcasting b->show(); //late binding b->print(); //early binding } 3. DownCasting.cpp /* DownCasting : derived class pointer pointing to base class object is illegal and and it technically termed as downcasting. */ #include<iostream> using namespace std; class base { int a; public: base() { a=10; } void print()
  • 156.
    { cout<<"In print:: Basen"; } //void show() // { // cout<<"In base t"<<"a :"<<a<<endl; // } virtual void show() { cout<<"In base t"<<"a :"<<a<<endl; } }; class der:public base { int b; public: der() { b=20; } void show() { cout<<"In derived t"<<"b :"<<b<<endl; } void print()
  • 157.
    { cout<<"In print::derivedn"; } }; int main() { baseb; der *d; d = &b; d->show(); d->print(); } ------------------------------------ March5,2018 chand sir not came today ---------------------------------- 1. abstraction_encapsulation_piggybank.cpp 2. Virtual_table.cpp ;;;; kindly google it 3.operator_overloading_for_New&delete.cpp 4.global_overloading_for_new&delete.cpp 5.operatoroverloading_withoutclass.cpp 6.pure_virtual_function.cpp 7.useof_initializerList_for_initializing_constVariable.cpp 8.pure_virtual_assignment.cpp
  • 158.
    1. abstraction_encapsulation_piggybank.cpp #include<iostream> using namespacestd; class piggybank { private: int rs5; int rs10; int rs20; int rs5_total, rs10_total, rs20_total,piggybnak_total; public: piggybank(int rs5 , int rs10, int rs20) { cout<<"nnnIn piggybank constructor getting notesn"; this->rs5 = rs5; this->rs10 = rs10; this->rs20 = rs20; } void total_amount() { cout<<"calculating total amountn"; rs5_total = rs5 * 5; rs10_total = rs10 * 10;
  • 159.
    rs20_total = rs20* 20; piggybnak_total = rs5_total+rs10_total+rs20_total; } void display() { cout<<"in displayn"; cout<<"rs5 total amount :"<<rs5_total<<"n"; cout<<"rs10 total amount :"<<rs10_total<<"n"; cout<<"rs20 total amount :"<<rs20_total<<"n"; cout<<"total amount in piggy bank :"<<piggybnak_total<<"n"; } ~piggybank() { cout<<"In piggybank destructorn"; } }; int main() { int rs5,rs10,rs20; cout<<"Enter total number of notes of rs5,rs10 and rs20n"; cin>>rs5>>rs10>>rs20;
  • 160.
    piggybank obj(rs5,rs10,rs20); obj.total_amount(); obj.display(); } 2. Virtual_table.cpp /* anyclass that has virtual function, a v table will be generated this v table will hold all the virtual function derived class has its own virtual table ,, whether we are creating or not base *ptr = derived_obj automatically compiler will assign 4 byte to ptr ,,, means memory will be increased by 4byte this ptr will hold the address of derived class show() address. */ 3. operator_overloading_for_New&delete.cpp /* size_t for type casting of long long int
  • 161.
    Syntax for overloadingthe new operator : void* operator new(size_t size); The overloaded new operator receives size of type size_t, which specifies the number of bytes of memory to be allocated. The return type of the overloaded new must be void*. The overloaded function returns a pointer to the beginning of the block of memory allocated. Syntax for overloading the delete operator : void operator delete(void*); The function receives a parameter of type void* which has to be deleted. Function should not return anything. NOTE: Both overloaded new and delete operator functions are static members by default. Therefore, they do not have access to this pointer . */ #include<iostream> #include<cstdlib> // c libraries can be used by writing c as prefix using namespace std; class student { private: string name; int age;
  • 162.
    public: student() { cout<<"in student constructorn"; } //student(string name,int age) // { // this->name = name; // this->age=age; // } student(string name,int age):name(name),age(age) { } void display() { cout<<"in display functionn"; cout<<"Name :"<<name<<", Age :"<<age<<endl; } void * operator new(size_t size) { cout<< "Overloading new operator with size: " << size << endl; //void * p = ::new student(); void * p = malloc(size); //will also work fine
  • 163.
    return p; } void operatordelete(void *p) { cout<< "Overloading delete operator " << endl; free(p); } }; int main() { student * p = new student("Yash", 24); p->display(); delete p; } 4. global_overloading_for_new&delete.cpp // CPP program to demonstrate // Global overloading of // new and delete operator #include<iostream> #include<stdlib.h>
  • 164.
    using namespace std; void* operator new(size_t size) { cout << "New operator overloading " << endl; void * p = malloc(size); return p; } void operator delete(void * p) { cout << "Delete operator overloading " << endl; free(p); } int main() { int n = 5, i; int * p = new int[3]; for (i = 0; i<n; i++) p[i]= i; cout << "Array: "; for(i = 0; i<n; i++) cout << p[i] << " ";
  • 165.
    cout << endl; deletep; } 5. pure_virtual_function.cpp /* pure virtual function is a function which does not have a body , and is always equating to zero. example virtual void show() = 0; any class which has at least one virtual function then that particular class will called as abstract base class ,,, and those abstract class are not allowed to create its object. usage: size of an object = size of non_static member + size of virtual function */ #include<iostream> using namespace std; class Base {
  • 166.
    protected: int x; public: virtual voidfun() = 0; // pure virtual function Base(int i) { x=i; } }; class Derived: public Base // this class inherits from base and implements fun() { int y; public: Derived(int i,int j):Base(i) // initializer list allowed us to initialize const and reference { y=j; } void fun() { cout<<"fun() calledn"; cout<<"x :"<<x<<" y :"<<y<<endl; } }; int main() { cout<<sizeof(class Base)<<endl;
  • 167.
    Derived d(10,20); d.fun(); // Baseobj; // cannot declare variable 'obj' to be of abstrct type 'Base' /* pointer to abstract class can be created but creating obj of base class is not allowed */ Base *ptr = &d; } 6. useof_initializerList_for_initializing_constVariable.cpp #include<iostream> using namespace std; /* class Base { protected: const int x; public: virtual void fun() = 0; // pure virtual function Base(int i) { x=i;
  • 168.
    } }; */ /* if a variableis declared as const then it have to be initialized at the time of declaration ,, otherwise it will take any garbage value ,,, and it can not be initialized further but using the initializer list a const variable can be initializing even after the declaration */ class Base { protected: const int x; public: virtual void fun() = 0; // pure virtual function Base(int i):x(i) { } }; class Derived: public Base // this class inherits from base and implements fun() { int y;
  • 169.
    public: Derived(int i,int j):Base(i)// initializer list allowed us to initialize const and reference { y=j; } void fun() { cout<<"fun() calledn"; cout<<"x :"<<x<<" y :"<<y<<endl; } }; int main() { cout<<sizeof(class Base)<<endl; Derived d(10,20); d.fun(); } 7. pure_virtual_assignment.cpp #include<iostream> #include<cstring> #include<string.h> using namespace std; class AbstractString
  • 170.
    { protected: char str[20]; public: AbstractString() { } AbstractString(const char*s1) { cout<<"IN base class constructor initializing strn"; strcpy(str,s1); } ~AbstractString() { } virtual void display() =0; }; class UpperString:public AbstractString { public: UpperString() {
  • 171.
    } UpperString(const char* s1):AbstractString(s1) { cout<<"inupper string n"; //strcpy(str,s1); } ~UpperString() { } void display() { cout<<"Uppercase string :"<<strupr(str); } }; class LowerString:public AbstractString { public: LowerString() { } LowerString(const char* s1):AbstractString(s1) {
  • 172.
    cout<<"in lowerStringn"; //strcpy(str,s1); } ~LowerString() { } void display() { cout<<"Lowercasestring :"<<strlwr(str); } }; int main() { char s1[20],s2[20]; cout<<"Enter a lower case stringn"; cin>>s1; UpperString objec(s1); AbstractString *obj=NULL; UpperString uprobj; obj = &uprobj; obj->display(); //*********************************************** cout<<"nnEnter a upper case stringn";
  • 173.
    cin>>s2; LowerString objec1(s2); AbstractString *obj1; LowerStringlwrobj; obj1 = &lwrobj; obj1->display(); } /* ----------------------------------------------- March6, 2018 ---------------------------------------------------- 1.Templets.cpp 2.templates1.cpp 3.templates2.cpp 4.swap_using_templates.cpp 5.maxOf2_usingTemplates_ternary_operator.cpp 6.calculator_templates.cpp 7.calculator_templates_outside class.cpp 8.STL.cpp 9.queue.cpp 10.static_in_template.cpp 1. Templates.cpp /* nm tool --- for mangling
  • 174.
    C++filt _Z3adddd -----to check mangling */ /* #include<iostream> using namespace std; void add(int a, int b) { cout<<"a :"<<a <<" ,b :"<<b<<" ,a+b :"<<a+b<<endl; } void add(double a, double b) { cout<<"a :"<<a<< " ,b :"<<b<<" ,a+b :"<<a+b<<endl; } int main() { add(2,3); add(1.2,2.3); } */ /* Templates: instead of writing add() multiple times we can write it only once and executes many times for different data type.(means re usability purpose)
  • 175.
    templates allows usto perform generic programming. Templates are of two type : standard template library and user defined templates STL : have various classes 1. QUeue 2.List 3.vector 4.map user defined templates: 1.function 2.class template */ #include<iostream> using namespace std; template<class T> // here class is a keyword which works as place holder for data type void add(T a, T b) { cout<<"a :"<<a<<" b :"<<b<<" a+b :"<<a+b<<endl;
  • 176.
    } int main() { add(1,2); add(1.2,2.3); } 2. templates1.cpp #include<iostream> usingnamespace std; template<class T, typename T1> // here class is a keyword which works as place holder for data type void add(T a, T1 b) { cout<<"a :"<<a<<" b :"<<b<<" a+b :"<<a+b<<endl; } int main() { add(1,2); add(11.2,12.3); }
  • 177.
    3. templates2.cpp #include<iostream> using namespacestd; template<class T, typename T1> // here class is a keyword which works as place holder for data type T1 add(T a, T1 b) { return (a+b); } int main() { cout<<"res :"<<add<double,double>(11.2,12.3)<<endl; cout<<"res :"<<add<int>(11,12)<<endl; cout<<"res :"<<add<char>(11,'a')<<endl; // implicit conversion } 4. swap_using_templates.cpp #include<iostream> using namespace std; template<class T, typename T1> void swp(T a, T1 b) {
  • 178.
    T1 c; c =a; a = b; b = c; cout<<"a= "<<a<<" b = "<<b<<endl; } int main() { int a=10; float b=12; swp(a,b); } 5. maxOf2_usingTemplates_ternary_operator.cpp #include<iostream> using namespace std; template<class T> T maxof2(T a, T b) { //(a>b)? return(a):return(b); return (a>b?a:b); } int main()
  • 179.
    { int a=12, b=24; cout<<"maxamong a :"<<a<<" ,b :"<<b<<" is :"<<maxof2(a,b)<<endl; } 6. calculator_templates.cpp #include<iostream> using namespace std; template<class T> class calculator { T a,b; public: calculator(T x,T y):a(x),b(y) { cout<<"Inside constructorn"; } ~calculator() { cout<<"inside destructorn"; } T add() { return (a+b); } T sub()
  • 180.
    { if(a>b) { return (a-b); } else return (b-a); } Tmul() { return (a*b); } T div() { return (a/b); } }; int main() { // student obj(5,2); calculator<double> obj(5,2); cout<<"In mainn"; cout<<"add(5,2) :"<<obj.add()<<endl; cout<<"sub(5,2) :"<<obj.sub()<<endl; cout<<"mul(5,2) :"<<obj.mul()<<endl; cout<<"div(5,2) :"<<obj.div()<<endl; }
  • 181.
    7. calculator_templates_outside class.cpp #include<iostream> usingnamespace std; template<class T> class calculator { T a,b; public: calculator(T x, T y); T add(); T sub(); T mul(); T div(); ~calculator() { cout<<"inside destructorn"; } }; template<class T> calculator<T>::calculator(T x,T y) { cout<<"Inside constructorn"; a=x; b=y;
  • 182.
    } template<class T> T calculator<T>::add() { return (a+b); } template<class T> T calculator<T>::sub() { if(a>b) { return (a-b); } else return (b-a); } template<class T> T calculator<T>::mul() { return (a*b); } template<class T> T calculator<T>:: div()
  • 183.
    { return (a/b); } int main() { //student obj(5,2); calculator<double> obj(5,2); cout<<"In mainn"; cout<<"add(5,2) :"<<obj.add()<<endl; cout<<"sub(5,2) :"<<obj.sub()<<endl; cout<<"mul(5,2) :"<<obj.mul()<<endl; cout<<"div(5,2) :"<<obj.div()<<endl; } 8. STL.cpp /* STL: standard template library 3 element: containers , iterators and algorithms containers are something which are capable to store something. containers are of type: total 16 containers are there. sequential ,ordered,unordered, ------------- iterator are used to travel through containers. forward iterator, backward iterator,control iterator
  • 184.
    ------------------ algorithms: */ #include<iostream> #include<vector> using namespace std; intmain() { vector<int> vobj; vobj.push_back(10); vobj.push_back(20); vobj.push_back(30); cout<<"Displaying content of vector :n"; if(vobj.empty()) cout<<"vector is emptyn"; else { for(int i=0;i<vobj.size();i++) // simple iterator using for loop { cout<<vobj[i]<<endl; } }
  • 185.
    // creating twovector with two object in heap segment(since its dynamic) vector<char> vobj1; vobj1.push_back('a'); vobj1.push_back('c'); vobj1.push_back('b'); cout<<"Displaying content of vector :n"; for(int i=0;i<vobj1.size();i++) // here object is indexed based { cout<<vobj1[i]<<endl; } // -------------- forward iterator : left begin---------------- cout<<"forward iteratorn"; vector<int>:: iterator lt; for(lt = vobj.begin();lt!=vobj.end(); lt++) // here iterator is pointer based { cout<<*lt<<endl; } // -------------- reverse iterator :right begin ---------------- cout<<"reverse iteratorn"; vector<int>:: reverse_iterator rt; for(rt = vobj.rbegin();rt!=vobj.rend(); rt++) // here iterator is pointer based
  • 186.
    { cout<<*rt<<endl; } cout<<"printing from secondelementn"; for(rt = vobj.rbegin()+1;rt!=vobj.rend(); rt++) // here iterator is pointer based { cout<<*rt<<endl; } // ------------ front AND BACK cout<<"vobj.front() :"<<vobj.front()<<" vobj1.back() :"<<vobj1.back()<<endl; cout<<"vobj.size() :"<<vobj.size()<<endl; vobj.pop_back(); cout<<"pop_back() iteratorn"; for(lt = vobj.begin();lt!=vobj.end(); lt++) // here iterator is pointer based { cout<<*lt<<endl; } // ------------------- sum and delete until list is not empty --------------------
  • 187.
    cout<<"vobj.size() :"<<vobj.size()<<endl; cout<<"sum ofall element :n"; int sum = 0; // for(lt = vobj.begin();lt!=vobj.end(); lt++) // here iterator is pointer based // { // sum = sum + (*lt); // // } //while(vobj.size()!=0) while(!vobj.empty()) { sum = sum + vobj.back(); vobj.pop_back(); } cout<<sum; } 9. queue.cpp /* Adapter class here queue is a vector class */
  • 188.
    #include<iostream> #include<vector> #include<queue> using namespace std; voidshowq(queue<int> gq) { queue<int> g = gq; while(!g.empty()) { cout<<'t'<<g.front(); g.pop(); } cout<<"n"; } int main() { queue<int> q; q.push(10); q.push(20); q.push(30); q.push(40); cout<<"The queue is :n"; showq(q); cout<<"nsize :"<<q.size();
  • 189.
    cout<<"nfront :"<<q.front(); cout<<"nback :"<<q.back(); cout<<"npop():"; q.pop(); showq(q); } 10. static_in_template.cpp #include<iostream> using namespace std; template<class T> class test { private: T val; public: static int count; test() { count++; } }; template<class T> int test<T>::count = 0;
  • 190.
    int main() { test<int> a; test<int>b; test<double> c; cout<<test<int>::count <<endl; cout<<test<double>::count <<endl; } ----------------------------------------------------------------- END -------------------------------------------------------------