-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a73a4f8
commit d863581
Showing
12 changed files
with
449 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
// /* | ||
// Intrduction of Constructor | ||
|
||
// Constructor is a special member function of a class which is used to initialize the objects of its class. | ||
|
||
// It is special because its name is the same as the class name. | ||
|
||
// It is invoked whenever an object of its associated class is created. | ||
|
||
// It is constructor’s responsibility to initialize the object of its class. | ||
|
||
// It has no return type, not even void. | ||
// */ | ||
|
||
class Student | ||
{ | ||
|
||
int roll; | ||
string name; | ||
int sem; | ||
int total_marks; | ||
|
||
public: | ||
Student(){ | ||
cout<<"Default Constructor Called"<<endl; | ||
} | ||
|
||
Student(int a,int b,int c){ | ||
cout<<"The sum of Given Number is : "<<a+b+c<<endl; | ||
} | ||
Student(int roll1, string name1, int sem1, int total_m) | ||
{ | ||
roll = roll1; | ||
name = name1; | ||
sem = sem1; | ||
total_marks = total_m; | ||
} | ||
void get_data() | ||
{ | ||
cout << "Roll no. : " << roll << endl; | ||
cout << "Name : " << name << endl; | ||
cout << "Semester : " << sem << endl; | ||
cout << "Total Marks : " << total_marks << endl; | ||
} | ||
|
||
~Student(){ | ||
cout<<"Destructor is called\n"; | ||
} | ||
}; | ||
int main() | ||
{ | ||
// Printing by Normal Method | ||
Student s1; | ||
// s1.set_data(1, "Yash", 3, 100); | ||
// s1.get_data(); | ||
|
||
// Printing with the help of Constructor | ||
Student s2(1, "Yash", 3, 100); | ||
// s1.set_data(1, "Yash", 3, 100); | ||
s2.get_data(); | ||
|
||
return 0; | ||
} | ||
|
||
// E:\Programming\CODING 2.0\C++\constr.cpp |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Creating a class | ||
|
||
/* | ||
Destructor | ||
It is Just Opposite to Constructor with (~). | ||
It is automatically Invoked when object goes out of scope. | ||
It has No return Type | ||
*/ | ||
class Employee | ||
{ | ||
string name; | ||
int id; | ||
int salary; | ||
|
||
public: | ||
Employee() | ||
{ | ||
cout << "This is a default constructor" << endl; | ||
} | ||
|
||
Employee(int id, int salary, string name) | ||
{ | ||
this->name = name; | ||
this->salary = salary; | ||
this->id = id; | ||
} | ||
|
||
void get_data() | ||
{ | ||
cout << "The name is : " << name << endl; | ||
cout << "The Id is : " << id << endl; | ||
cout << "The Salary is : " << salary << endl; | ||
} | ||
|
||
~Employee(){ | ||
cout<<"I'm a Destructor and now I'm called\n"; | ||
} | ||
}; | ||
|
||
// Main Function -> Code Execution Starts here | ||
int main() | ||
{ | ||
// Creating Obj | ||
// Class_name obj_name | ||
Employee e1(12, 10000, "Yash"); | ||
e1.get_data(); | ||
|
||
Employee e2; | ||
Employee e3; | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
/* | ||
class <derived_class_name> : <access-specifier> <base_class_name> | ||
{ | ||
//body | ||
} | ||
*/ | ||
// Single Inheritance | ||
// Base Class | ||
class Parent | ||
{ | ||
public: | ||
int a; | ||
int b; | ||
void get_data() | ||
{ | ||
cout<<"Enter the value of a and b\n"; | ||
cin>>a>>b; | ||
} | ||
void show_data() | ||
{ | ||
cout<<"The value of a is : "<<a<<endl; | ||
cout<<"The value of b is : "<<b<<endl; | ||
} | ||
}; | ||
|
||
|
||
// Derived Class | ||
|
||
// In Inheritance | ||
/* | ||
If you are Inheriting publicly | ||
-> public member are public | ||
-> protected member private | ||
-> private member can never be Inherited | ||
If you are Inheriting protected way | ||
-> public member are public | ||
-> protected member private | ||
-> private member can never be Inherited | ||
*/ | ||
class Child : public Parent{ | ||
public: | ||
void sum(){ | ||
cout<<"The sum of a and b is : "<<a+b<<endl; | ||
} | ||
}; | ||
|
||
|
||
|
||
int main() | ||
{ | ||
Child c1; | ||
c1.get_data(); | ||
c1.show_data(); | ||
c1.sum(); | ||
cout<<c1.a<<endl; | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// #include <bits/stdc++.h> | ||
// using namespace std; | ||
|
||
// // Base class - 1 | ||
// class parent1 | ||
// { | ||
// public: | ||
// int num = 10; | ||
// string name = "Lokesh"; | ||
|
||
// }; | ||
|
||
// // Base Class - 2 | ||
// class parent2 | ||
// { | ||
// public: | ||
// int num1, num2; | ||
|
||
// public: | ||
// void get_data() | ||
// { | ||
// cout << "Enter the value of Num1 and Num2 " << endl; | ||
// cin >> num1 >> num2; | ||
// } | ||
|
||
// void show_data() | ||
// { | ||
// cout << "The Number1 is : " << num1 << endl; | ||
// cout << "The Number2 is : " << num2 << endl; | ||
// } | ||
// }; | ||
|
||
// // Child/Derived/Sub class | ||
|
||
// class Child1 : public parent1, public parent2 | ||
// { | ||
// public: | ||
// void sumParent2() | ||
// { | ||
// cout << "The sum of 2 Numbers of Parent2 class is : " << num1 + num2 << endl; | ||
// } | ||
// void print_data() | ||
// { | ||
// cout << "The name is : " << name << endl; | ||
// cout << "The number is : " << num << endl; | ||
// } | ||
// }; | ||
|
||
// int main() | ||
// { | ||
// Child1 c1; | ||
// c1.print_data(); | ||
|
||
// c1.get_data(); | ||
// c1.show_data(); | ||
|
||
// c1.sumParent2(); | ||
// return 0; | ||
// } | ||
|
||
// Multilevel Inheritance | ||
|
||
// #include <bits/stdc++.h> | ||
// using namespace std; | ||
// class Baba | ||
// { | ||
// protected: | ||
// int rupya1 = 1000; | ||
// int rupya2 = 10000; | ||
|
||
// Baba(){ | ||
// cout<<"I'm the constructor of papa's papa class\n"; | ||
// } | ||
// }; | ||
|
||
// class Papa : protected Baba | ||
// { | ||
// public: | ||
// int dollar = 5000; | ||
// Papa(){ | ||
// cout<<"I'm the constructor of papa class\n"; | ||
// } | ||
// }; | ||
|
||
// class hum : public Papa | ||
// { | ||
// public: | ||
// hum(){ | ||
// cout<<"My baba has "<<rupya1<<" "<<rupya2<<" as his salary"<<endl; | ||
// cout<<"My papa's salary in dollar is : "<<dollar<<endl; | ||
// } | ||
// }; | ||
// int main() | ||
// { | ||
|
||
// hum h1; | ||
|
||
// return 0; | ||
// } | ||
|
||
|
||
|
||
// Hierarchical Inheritance | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Parent class | ||
class Baba | ||
{ | ||
protected: | ||
int rupya1 = 1000; | ||
int rupya2 = 10000; | ||
|
||
}; | ||
|
||
// Child - 1 | ||
class Papa : protected Baba | ||
{ | ||
public: | ||
int dollar = 5000; | ||
Papa(){ | ||
cout<<"I'm the constructor of papa class\n"; | ||
} | ||
|
||
void show_data(){ | ||
cout<<"My baba has "<<rupya1<<" "<<rupya2<<" as his salary"<<endl; | ||
cout<<"My salary in dollars is : "<<dollar<<endl; | ||
} | ||
}; | ||
|
||
// Child 2 | ||
class hum : public Baba | ||
{ | ||
public: | ||
hum(){ | ||
cout<<"My baba has "<<rupya1<<" "<<rupya2<<" as his salary"<<endl; | ||
} | ||
}; | ||
|
||
// Main Class | ||
int main() | ||
{ | ||
// Child1 Object | ||
hum h1; | ||
|
||
// Child2 Object | ||
Papa p; | ||
p.show_data(); | ||
|
||
return 0; | ||
} |
Binary file not shown.
Oops, something went wrong.