Class, Object, And Method
#include <bits/stdc++.h>
using namespace std;
class MyClass
{
public:
int num;
string Str;
// Method
void method()
{
// Method--> function inside A class
cout << "Hello This is Method";
}
};
int main()
{
MyClass Obj;
Obj.num = 50;
Obj.Str = "This is Str\n";
cout << Obj.num << "\n";
cout << Obj.Str;
Obj.method();
return 0;
}
output
50
This is Str
Hello This is Method
Constructors and Access Specifiers
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes. You will learn more about Inheritance later.
#include <bits/stdc++.h>
using namespace std;
class Person
{
public: // Public access specifier
int x;
private: // Private access specifier
int y;
};
int main()
{
Person Obj;
Obj.x = 100;
Obj.y = 150; // Not access
return 0;
}
////////
#include <bits/stdc++.h>
using namespace std;
class Person
{ // The class
public:
string firstName;
string lastName;
int age;
// Constructor
Person(string x, string y, int z)
{
firstName = x;
lastName = y;
age = z;
}
};
int main()
{
Person Obj1("Rezaul", "Karim", 22);
cout << Obj1.firstName << " " << Obj1.lastName << " "
<< carObj1.age << "\n";
return 0;
}
output
Rezaul Karim 22
Encapsulation
#include <bits/stdc++.h>
using namespace std;
class Person
{
private:
string name;
public:
// void
void SetName(string x)
{
name = x;
}
string GetName()
{
return name;
}
};
int main()
{
Person Obj;
Obj.SetName("Rezaul Karim");
cout << Obj.GetName();
return 0;
}
output
Rezaul Karim
Inheritance
#include <bits/stdc++.h>
using namespace std;
// Person class
class Person
{
public:
string name;
int age;
// input function
void input()
{
cout << "Enter Your Name : ";
cin >> name;
cout << "Enter Your age : ";
cin >> age;
}
// display function
void display()
{
cout << "Name : " << name << endl;
cout << "age : " << age << endl;
}
};
// Student class
class Student : public Person // Student class inheritance Person class
{
public:
int roll;
// StudentInput function
void studentInput()
{
input(); // input function from Person class
cout << "Enter Your Roll : ";
cin >> roll;
}
// display function
void studentDisplay()
{
display(); // display function from Person class
cout << "Roll : " << roll << endl;
}
};
int main()
{
Student obj;
obj.studentInput();
obj.studentDisplay();
return 0;
}
output
Enter Your Name : rezaul
Enter Your age : 22
Enter Your Roll : 50
Name : rezaul
age : 22
Roll : 50
Polymorphism
#include <bits/stdc++.h>
using namespace std;
// Base class
class Person
{
public:
void personFunction()
{
cout << "This is person Function \n";
}
};
// Derived class
class Student : public Person
{
public:
void personFunction()
{
cout << "This is person Function From Student class\n";
}
};
// Derived class
class Student2 : public Person
{
public:
void personFunction()
{
cout << "This is person Function From Student2 class\n";
}
};
int main()
{
Person PersonDisplay;
Student StudentDisplay;
Student2 Student2Display;
PersonDisplay.personFunction();
StudentDisplay.personFunction();
Student2Display.personFunction();
return 0;
}
output
This is person Function
This is person Function From Student class
This is person Function From Student2 class
Abstraction
#include <bits/stdc++.h>
using namespace std;
class Person
{
private:
string name;
int age; // private variables
public:
void input()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
void display()
{
cout << "Name is : " << name << "\n Age is : " << age << endl;
}
};
int main()
{
Person obj;
obj.input();
obj.display();
return 0;
}
output
Enter name: reza
Enter age: 22
Name is : reza
Age is : 22
Files
ofstream --- Creates and writes to files
ifstream --- Reads from files
fstream -- A combination of ofstream and ifstream: creates, reads, and writes to files
#include <iostream>
#include <fstream>
using namespace std;
// Create and open a text file
int main()
{
string name;
ofstream file("Student.txt", ios::out | ios::app);
cout << "Enter Your Name : ";
getline(cin, name);
file << "welcome " << name << endl;
file.close();
return 0;
}
// Read text file
int main()
{
string line;
ifstream file("Student.txt", ios::out | ios::app);
while (getline(file, line))
{
cout << line << endl;
}
file.close();
return 0;
}
0 Comments