string Data Type Demonstration

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

//string data type demonstration.

int main()
{
string n;
cout<<"Enter the value\n";
cin>>n;
cout<<n;
cout<<"\n skipped after the space in the name......";
getch();
return 1;
}

// string with getline function to read complete line of string.


int main()
{
string str;
cout<<"What is your name:";
getline(cin, str);
cout<<"Welcome "<<str<<endl;
}
// Another example of getline function.
#include<iostream>
using namespace std;
#include<string>

int main()
{
string mystr;
cout << "What's your name? ";

getline(cin, mystr);

cout << "Hello " << mystr << ".\n";


cout << "Which is your favorite Cricket team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
// sorting of an array…
#define MAX 100
int main()
{
//array declaration
int arr[MAX];
int n,i,j;
int temp;
//read total number of elements to read
cout<<"Enter total number of elements to read: ";
cin>>n;
//read n elements
for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"] ";
cin>>arr[i];
}

//print input elements


cout<<"Unsorted Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;

//sorting - ASCENDING ORDER


for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

//print sorted array elements


cout<<"Sorted (Ascending Order) Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
getch();
return 0;
}
// MultiDimentional Array..
#define WIDTH 5
#define HEIGHT 3

int a[WIDTH][HEIGHT];
int n,m;
int main ()
{
for (n=0;n<HEIGHT;n++)
{
for (m=0;m<WIDTH;m++)
{
a[n][m]=(n+1)*(m+1);
cout<<a[n][m]<<'\t';
}
cout<<endl;
}

return 0;
}
// Another Example of MultiDimentional Array..
#include <iostream>
using namespace std;

int main() {
int numbers[2][3];

cout << "Enter 6 numbers: " << endl;

// Storing user input in the array


for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
cin >> numbers[i][j];
}
}

cout << "The numbers are: " << endl;

// Printing array elements


for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
// cout<<numbers[i][j]<<'\t';
cout << "numbers[" << i << "][" << j << "]: " << numbers[i][j] << endl;
}//cout<<endl<<endl;
}

return 0;
}
//Insertion of an Element into an array
#include<iostream>
using namespace std;
main()
{
int n=6;
int a[n], i, p, e;
cout<<"Enter the "<<n<<" Elemnts of an Array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nSpecify the Position";
cin>>p;
cout<<"\nEnter the Value\n";
cin>>e;
for(i=n-1; i>=p;i--)
{
a[i+1]=a[i];
}
a[p]=e;
for(i=0;i<=n;i++)
{
cout<<a[i]<<" ";
}
}
//Deletion of an Element from an array
#include<iostream>
using namespace std;
int main()
{
int n=5,p,i;
int a[n];
cout<<"enter the 5 elements of an array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nEntered Elements are\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}

cout<<"\n From which position you want to delete an element\n";


cin>>p;

if(p<=0 || p>n)
{
cout<<"Invalid Position. It should not be negative or zero";
}

else if(p==n)
{
n--;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
else
{
for(i=p-1;i<n;i++)
{
a[i]=a[i+1];
}
n--;
cout<<"\nAfter Deletion\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
return 1;

}
//C++ program to demonstrate function declaration outside class

#include <iostream>
using namespace std;
class Welcome
{
public:
string Course_Name;
int id;
void printname();
void printid()
{
cout << "Course id is: " << id;
}
};

// Definition of printname using scope resolution operator ::


void Welcome::printname()
{
cout << "Course is: " << Course_Name;
}

int main() {

Welcome obj1, obj2;


obj1.Course_Name = "Data Structures using C++";
obj1.id=15;

obj2.Course_Name = "Data Structures using C";


obj2.id=14;

obj1.printname();
cout << endl;
obj1.printid();
return 0;
}
//Single Inheritance
#include<iostream>
using namespace std;
class Employee {
public:
int salary;

};
class Developer : public Employee {
Employee e;
public:
void salary() {
cout<<"Enter employee salary: ";
cin>>e.salary;
cout<<"Employee salary: "<<e.salary;
}
};
int main() {

Developer obj;
obj.salary();
obj.name();
return 1;
}
/*C++ program to demonstrate example of simple inheritance.*/
#include <iostream>
using namespace std;

/*Base Class*/
class A
{
public:
void Afun(void);
};

// function definiion
void A::Afun()
{
cout << "I'm the Definition of Afun()..." << endl;
}
/*Derived Class*/
class B:public A
{
public:
void Bfun();
};
// function definition
void B::Bfun()
{
cout << "I'm the Definition of Bfun()..." << endl;
}

int main()
{
//create object of derived class - class B
B objB;
// Now, we can access the function of class A (Base class)
objB.Afun();
objB.Bfun();
return 0;
}
/*C++ program to demonstrate example of private simple inheritance.*/
class A
{
private:
int a;
protected:
int x; //can access by the derived class
public:
void setVal(int v)
{
x=v;
}
};

class B:private A
{
public:
void printVal()
{
setVal(10); //accessing public member function here
//protected data member direct access here
cout << "value of x: " << x << endl;
}
};

int main()
{
B objB; //derived class creation
objB.printVal();
return 0;
}
/* C++ program to demonstrate example of multilevel inheritance.*/

#include <iostream>
using namespace std;

//Base Class : class A


class A
{
private:
int a;
public:
void get_a(int val_a)
{
a=val_a;
}

void disp_a()
{
cout << "Value of a: " << a << endl;
}
};
//Here Class B is base class for class C and Derived class for class A
class B: public A
{
private:
int b;
public:
//assign value of a from here
void get_b(int val_a, int val_b)
{
//assign value of a by calling function of class A
get_a(val_a);
b=val_b;
}

void disp_b()
{
//display value of a
disp_a();
cout << "Value of b: " << b << endl;
}
};
//Here class C is derived class and B is Base class
class C: public B
{
private:
int c;
public:
//assign value of a from here
void get_c(int val_a, int val_b,int val_c)
{
/*** Multilevel Inheritance ***/
//assign value of a, bby calling function of class B and Class A
//here Class A is inherited on Class B, and Class B in inherited on Class B
get_b(val_a,val_b);
c=val_c;
}

void disp_c()
{
//display value of a and b using disp_b()
disp_b();
cout << "Value of c: " << c << endl;
}
};

int main()
{
//create object of final class, which is Class C
C objC;

objC.get_c(10,20,30);
objC.disp_c();

return 0;
}
/* C++ program to read and print employee information using multiple inheritance.*/

//Base Class - basicInfo


class basicInfo
{
protected:
char str[30];
int empId;
char gender;
public:
void getBasicInfo()
{
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
// cin.ignore(1);
cout << "Enter Name: ";
cin.getline(str,30);
}
};

//Base Class - deptInfo


class deptInfo
{
protected:
char deptName[30];
char assignedWork[30];
int time2complete;
public:
void getDeptInfo()
{

cout << "Enter Department Name: ";


cin.ignore(1);
cin.getline(deptName,30);
cout << "Enter assigned work: ";
cin.getline(assignedWork,30);//cin.getline(assignedWork,30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;

}
};

/*final class (Derived Class)- employee*/


class employee:private basicInfo, private deptInfo
{
public:
void getEmployeeInfo(){
cout << "Enter employee's basic info: " << endl;
//call getBasicInfo() of class basicInfo
getBasicInfo(); //calling of public member function
cout << "Enter employee's department info: " << endl;
//call getDeptInfo() of class deptInfo
getDeptInfo(); //calling of public member function
}
void printEmployeeInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << str << endl; //accessing protected data
cout << "Employee ID: " << empId << endl; //accessing protected data
cout << "Gender: " << gender << endl << endl;//accessing protected data

cout << "Department Information...:" << endl;


cout << "Department Name: " << deptName << endl; //accessing protected data
cout << "Assigned Work: " << assignedWork << endl; //accessing protected data
cout << "Time to complete work: " << time2complete<< endl; //accessing protected data
}
};

int main()
{
//create object of class employee
employee emp;

emp.getEmployeeInfo();
emp.printEmployeeInfo();

return 0;
}
/*C++ program to read and print employee information with department and pf information using
hierarchical inheritance.*/

#include <iostream>
#include <stdio.h>
using namespace std;

//Base Class - basicInfo


class basicInfo
{
protected:
char name[30];
int empId;
char gender;
public:
void getBasicInfo(void)
{
cout << "Enter Name: ";
cin.getline(name,30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};

//Base Class - deptInfo


class deptInfo: private basicInfo
{
protected:
char deptName[30];
char assignedWork[30];
int time2complete;
public:
void getDeptInfo(void)
{
getBasicInfo(); //to get basic info of an employee
cout << "Enter Department Name: ";
cin.ignore(1);
cin.getline(deptName,30);
cout << "Enter assigned work: ";

cin.getline(assignedWork,30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
void printDeptInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl; //accessing protected data
cout << "Employee ID: " << empId << endl; //accessing protected data
cout << "Gender: " << gender << endl << endl;//accessing protected data

cout << "Department Information...:" << endl;


cout << "Department Name: " << deptName << endl; //accessing protected data
cout << "Assigned Work: " << assignedWork << endl; //accessing protected data
cout << "Time to complete work: " << time2complete<< endl; //accessing protected data

}
};

//another Base Class : loadInfo


class loanInfo:private basicInfo
{
protected:
char loanDetails[30];
int loanAmount;
public:
void getLoanInfo(void)
{
getBasicInfo(); //to get basic info of an employee
cout << "Enter Loan Details: ";
cin.ignore(1);
cin.getline(loanDetails,30);
cout << "Enter loan amount: ";
cin >> loanAmount;
}
void printLoanInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl; //accessing protected data
cout << "Employee ID: " << empId << endl; //accessing protected data
cout << "Gender: " << gender << endl << endl;//accessing protected data

cout << "Loan Information...:" << endl;


cout << "Loan Details: " << loanDetails << endl; //accessing protected data
cout << "Loan Amount : " << loanAmount << endl; //accessing protected data
}
};

int main()
{
//read and print department information
deptInfo objD;

objD.getDeptInfo();
objD.printDeptInfo();

cout << endl << endl ;


//read and print loan information
loanInfo objL;

objL.getLoanInfo();
objL.printLoanInfo();

return 0;
}

You might also like