Basic OOP Tutorial : C++ Programming | Object Oriented
Basic Programming Tutorial for C++
I found this in my old storage and I would like to share this to you. Here is an example of how to do a simple and basic object oriented programming in C++. Please be guided accordingly and follow the important points used in the objected oriented programming source code for C++. You could also customize your own file name, variables, method names and etc. Be playful and explore as possible.
Output:
Employee Information
Name:
Age:
Weight:
Height:
Salary:
Input:
- Name (string)
- Age (int)
- Weight (int)
- Height (double)
- Salary (double)
Note: You can also customize your own variables
Object Oriented Programming (OOP)
Your customized header file:
#include “Employee.h”
Note:
initialize class Employee first before using the methods created > Employee e;
Process that You Should Remember:
- Include your header at the top (for the .cpp file)
- Initialize the variables
- Initialize the class you created (in .h file)
- Set up Input
- Call all set methods to set the values of the specific variables in the header
- Print all or call the print method to view the employee information
Main File (.cpp)
C++ Programming Tutorial: Object Oriented
//--www.skellainnovations.com--//
#include<iostream>
#include<conio.h>
#include "Employee.h"
using namespace std;
main ()
{
string n;
int a, w;
double h, s;
Employee e;
cout<<"Enter Name: "; cin>>n;
cout<<"Enter Age: "; cin>>a;
cout<<"Enter Weight: "; cin>>w;
cout<<"Enter Height: "; cin>>h;
cout<<"Enter Salary: "; cin>>s;
e.setName(n);
e.setAge(a);
e.setWeight(w);
e.setHeight(h);
e.setSalary(s);
e.print();
getch();
}
//--www.skellainnovations.com--//
Header File:
(Employee.h) will serve as your customized library
Private:
all variables
Public:
all methods
Methods:
- void print();
- void setName(string n);
- void setAge(int a);
- void setWeight(int w);
- void setHeight(double h);
- void setSalary(double s);
- string getName();
- int getAge();
- int getWeight();
- double getHeight();
- double getSalary();
Constructors:
- Employee();
- Employee(string n, int a, int w, double h, double s);
Header File Content: Basic Object Oriented
//---www.skellainnovations.com---//
#include<iostream>
#include<conio.h>
using namespace std;
class Employee{
private:
string name;
int age;
int weight;
double height;
double salary;
public:
void print();
void setName(string n);
void setAge(int a);
void setWeight(int w);
void setHeight(double h);
void setSalary(double s);
string getName();
int getAge();
int getWeight();
double getHeight();
double getSalary();
Employee();
Employee(string n, int a, int w, double h, double s);
};
Employee::Employee(){
name=" ";
age=0;
weight=0;
salary=0;
height=0;
}
Employee::Employee(string n, int a, int w, double h, double s){
name=n;
age=a;
weight=w;
height=h;
salary=s;
}
void Employee::setName(string n){
name=n;
}
string Employee::getName(){
return name;
}
void Employee::setAge(int a){
age=a;
}
int Employee::getAge(){
return age;
}
void Employee::setWeight(int w){
weight=w;
}
int Employee::getWeight(){
return weight;
}
void Employee::setHeight(double h){
height=h;
}
double Employee::getHeight(){
return height;
}
void Employee::setSalary(double s){
salary=s;
}
double Employee::getSalary(){
return salary;
}
void Employee::print(){
cout<<endl<<"Employee Information"<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Weight: "<<weight<<endl;
cout<<"Height: "<<height<<endl;
cout<<"Salary: "<<salary<<endl;
getch();
}
//---www.skellainnovations.com---//
You may also visit : Top 5 Common Problems in C++ << CLICK HERE




