0% found this document useful (0 votes)
11 views50 pages

2 C++

About c++ concepts

Uploaded by

bhavsammu37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views50 pages

2 C++

About c++ concepts

Uploaded by

bhavsammu37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

10 students grade

#include<iostream>
#include<string>
using namespace std;
class student
{
private:
string name;
int num,m1,m2,m3;
double avg;
char grade;
public:
void setdata(string na,int n,int m11,int m22,int m33)
{
name=na;
num=n;
m1=m11;
m2=m22;
m3=m33;
avg=a();
grade=g();
}
double a()
{
return (m1+m2+m3)/3.0;
}
char g()
{
if (avg > 90) {
return 'S';
} else if (avg > 80) {
return 'A';
} else if (avg > 70) {
return 'B';
} else if (avg > 60) {
return 'C';
} else if (avg > 50) {
return 'D';
} else {
return 'F';
}
}
void display()
{
cout<<name<<endl;
cout<<num<<endl;
cout<<m1<<endl;
cout<<m2<<endl;
cout<<m3<<endl;
cout<<avg<<endl;
cout<<grade;
}
};
int main()
{
const int to=2;
student s1[to];
for(int i=0;i<to;i++)
{
string name;
int num,m1,m2,m3;
cin>>name>>num>>m1>>m2>>m3;
s1[i].setdata(name,num,m1,m2,m3);
}
for(int i=0;i<to;i++)
{
s1[i].display();
}

Inheritance:
#include<iostream>
using namespace std;
class shape
{
protected:
int l,b;
public:
shape(int le,int br)
{
l=le;
b=br;
}
};
class re:public shape
{
public:
re(int le,int br):shape(le,br){}
int a()
{
return l*b;
}
};
int main()
{
int l,b;
cin>>l>>b;
re r(l,b);
cout<<r.a();
}

Or
#include<iostream>
using namespace std;
class shape
{
protected:
int l,b;
public:
shape(int le,int br)
{
l=le;
b=br;
}

};
class re:public shape
{
private:
int r;

public:
re(int le,int br):shape(le,br){}
public:
re(int ra):shape(0,0)
{
r=ra;
}
int a()
{
return l*b;
}
int c()
{
return r*r;
}
};
int main()
{
int l,b,r;
cin>>l>>b>>r;
re r0(l,b);
re r1(r);
cout<<r1.c()<<endl;
cout<<r0.a();
}

Or
#include<iostream>
using namespace std;
class p
{
protected:
int a, c;
public:
p(int a1)
{
a=a1;

}
p(int a1,int c1):p(a1)
{
c=c1;
}
};
class sq:public p
{
public:
sq(int a1):p(a1){
}
int n()
{
return a*a;
}
};
class cu:public p
{
public:
cu(int c1):p(0,c1){
}
int x()
{
return c*c*c;
}
};
int main()
{
int a,c;
cin>>a>>c;
sq sq1(a);
cu cu1(c);
cout<<sq1.n();
cout<<cu1.x();
}

Patient

#include<iostream>
#include<string>
using namespace std;
class person
{
protected:
string name;
int age;
public:
person(string n,int a)
{
name=n;
age=a;
}
void display()
{
cout<<name<<endl;
cout<<age<<endl;
}
};
class patient:public person
{
private:
int id;
string d;
public:
patient(string n,int a,int idd,string di):person(n,a){
id=idd;
d=di;
}
void dis()
{
display();
cout<<id<<endl;
cout<<d;
}
};
int main()
{
string name,d;
int age,id;
cin>>name>>d>>age>>id;
cin.ignore();
patient p(name,age,id,d);
p.dis();

Object count:
#include <iostream>
using namespace std;
class MyClass {
private:
static int objectCount;

public:
MyClass() {
objectCount++;
}
static int getObjectCount() {
return objectCount;
}
};

int MyClass::objectCount = 0;

int main() {
MyClass obj1;

cout << MyClass::getObjectCount() ;

return 0;
}

One student grade:

#include<iostream>
#include<string>
using namespace std;
class student
{
private:
string name;
int num,m1,m2,m3;
double avg;
char grade;
public:
void setdata(string na,int n,int m11,int m22,int m33)
{
name=na;
num=n;
m1=m11;
m2=m22;
m3=m33;
avg=a();
grade=g();
}
double a()
{
return (m1+m2+m3)/3.0;
}
char g()
{
if (avg > 90) {
return 'S';
} else if (avg > 80) {
return 'A';
} else if (avg > 70) {
return 'B';
} else if (avg > 60) {
return 'C';
} else if (avg > 50) {
return 'D';
} else {
return 'F';
}
}
void display()
{
cout<<name<<endl;
cout<<num<<endl;
cout<<m1<<endl;
cout<<m2<<endl;
cout<<m3<<endl;
cout<<avg<<endl;
cout<<grade;
}
};
int main()
{
student s1;
string name;
int num,m1,m2,m3;
cin>>name>>num>>m1>>m2>>m3;
s1.setdata(name,num,m1,m2,m3);
s1.display();
}
Pal
#include<iostream>
#include<string>
using namespace std;
class pal
{
private:
string n;
public:
void setn(string nu)
{
n=nu;
}
string pali()
{
string t="";
for(int i=n.length()-1;i>=0;i--)
{
t+=n[i];
}

if(n==t)
{
cout<<"s";
}
else
{
cout<<"n";
}
}

};
int main()
{
pal p;
string m;
cin>>m;
p.setn(m);
cout<<p.pali();

Class palindrome

#include<iostream>

#include<string>

using namespace std;

class pal

{
private:

string n;

public:

void setn(string nu)

n=nu;

string pali()

string t="";

for(int i=n.length()-1;i>=0;i--)

t+=n[i];

return t;

};

int main()

pal p;

string m;

cin>>m;

p.setn(m);

cout<<p.pali();

Constructor :
#include<iostream>
using namespace std;
class room
{
private:
int l,b;
public:
room (int le,int br)
{
l=le;
b=br;
}
int a()
{
return l*b;
}
};
int main()
{
int a,b;
cin>>a>>b;
room r(a,b);
cout<<r.a();

}
Or

#include<iostream>
using namespace std;
class room
{
private:
int l,b;

public:
room (int le,int br)
{
l=le;
b=br;
}

int a()
{
return l*b;
}

};
class t
{
private:
int ba,h;
public:
t(int bas,int he)
{
ba=bas;
h=he;
}
int tr()
{
return 0.5*ba*h;
}
};
int main()
{

int a,b;
cin>>a>>b;
room r(a,b);

cout<<r.a()<<endl;
cout<<"t"<<endl;
int x,y;
cin>>x>>y;
t ti(x,y);
cout<<ti.tr();

Or

#include<iostream>
using namespace std;
class rec
{
private:
int l,b;
public:
rec(int le,int br)
{
l=le;
b=br;
}
rec()
{
l=0;
b=0;
}
rec(int a)
{
l=a;
b=a;
}
int ar()
{
return l*b;
}
};
int main()
{
rec r(5,10);
rec r1(5);
rec r2;
cout<<r.ar()<<endl;
cout<<r1.ar()<<endl;
cout<<r2.ar()<<endl;
}

Or

#include<iostream>
using namespace std;
class rec
{
private:
int l,b;
public:
rec(int le,int br)
{
l=le;
b=br;
}
rec()
{
l=0;
b=0;
}
rec(int a)
{
l=a;
b=a;
}
int ar()
{
return l*b;
}
};
int main()
{
int l,b,a;
cin>>l>>b>>a;
rec r(l,b);
rec r1(a);
rec r2;
cout<<r.ar()<<endl;
cout<<r1.ar()<<endl;
cout<<r2.ar()<<endl;
}

Piggy

#include<iostream>
using namespace std;
class piggy
{
private:
int a;
public:
piggy(int am)
{
a=am+50;
}
piggy()
{
a=50;
}
int amount()
{
return a;
}
};
int main()
{
int a;
cin>>a;
piggy r(a);
piggy r2;
cout<<r.amount()<<endl;
cout<<r2.amount()<<endl;
}

String
#include<iostream>
#include<string>
using namespace std;
class piggy
{
private:
string a;
public:
piggy(string am)
{
a=am;
}
piggy()
{
a="unknown";
}
string name()
{
return a;
}
};
int main()
{
string a;
cin>>a;
piggy r(a);
piggy r2;
cout<<r.name()<<endl;
cout<<r2.name()<<endl;
}

Si:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int p,y,i;
cin>>p>>y;
string k;
cout<<"y or n";
cin>>k;
if(k=="y")
{
i=(p*y*12)/100;
cout<<i;
}
else if(k=="n")
{
i=(p*y*10)/100;
cout<<i;
}
else
{
cout<<"no";
}
}

Bonus:

#include<iostream>
#include<string>
using namespace std;
int main()
{
int b,s,t;
cin>>s;
string k;
cout<<"a or b";
cin>>k;
if(k=="a")
{
if(s<10000)
{
b=(s*7)/100;
t=b+s;
cout<<b<<endl;
cout<<t;
}
else
{
b=(s*5)/100;
t=b+s;
cout<<b<<endl;
cout<<t;
}
}
else if(k=="b")
{
if(s<10000)
{
b=(s*12)/100;
t=b+s;
cout<<b<<endl;
cout<<t;
}
else
{
b=(s*10)/100;
t=b+s;
cout<<b<<endl;
cout<<t;
}
}
else
{
cout<<"no";
}
}

Lower and upper

#include<iostream>
#include<string>
using namespace std;
int main()
{
string n;
cin>>n;
for(int i=0;i<n.length();i++)
{
n[i]=tolower(n[i]);
}
cout<<n;
for(int i=0;i<n.length();i++)
{
n[i]=toupper(n[i]);
}
cout<<n;
}

Sum of natual
#include<iostream>

using namespace std;


int main()
{
int c,s=0;
cin>>c;
for(int i=0;i<c;i++)
{
int num;
cin>>num;
s+=num;
}
cout<<s;
}

Sum of first n:
#include<iostream>
using namespace std;
int main()
{
int c,s=0;
cin>>c;
for(int i=1;i<=c;i++)
{
s+=i;
}
cout<<s;
}

Nth fib
#include<iostream>
using namespace std;
int fib(int n)
{
if(n<=0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
int a=0,b=1;
for(int i=2;i<=n;i++)
{
int c=a+b;
a=b;
b=c;
}
return b;
}
}
int main()
{
int n;
cin>>n;
int r=fib(n);
cout<<r;
}

Power
#include<iostream>
using namespace std;
int main()
{
int b;
double e,r=1.0;
cin>>b>>e;
if(e>=0)
{
for(int i=1;i<=e;i++)
{
r=r*b;
}
}
else
{
for(int i=1;i<=-e;i++)
{
r=r/b;
}
}
cout<<r;
}

Function age:
#include<iostream>
using namespace std;
int get()
{
int a;
cin>>a;
return a;
}
int main()
{
int a=get();
if(a>=18)
{
cout<<"e";
}
else
{
int d=a-18;
cout<<"n";
cout<<d<<"more yrs";
}
}

Si function:
#include<iostream>
using namespace std;
void get(int &p,int &y,string &k)
{
cin>>p>>y;
cout<<"yor n";
cin>>k;
}
int main()
{
int p,y,i;
string k;
get(p,y,k);
if(k=="y")
{
i=(p*y*12)/100;
cout<<i;
}
else if(k=="n")
{
i=(p*y*10)/100;
cout<<i;
}
}

Default:
#include<iostream>
using namespace std;
int add(int a, int b,int c,int d)
{
return a+b+c+d;
}
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<add(a,b,c,d);
}

Switch:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
char o;
cout<<"A or B /C /D";
cin>>o;
switch(o)
{
case 'A':
c=a+b;
cout<<c;
break;
case 'B':
c=a-b;
cout<<c;
break;
case 'C':
c=a*b;
cout<<c;
break;
case 'D':
c=a/b;
cout<<c;
break;
default:
cout<<"in";
}

Employe:
#include<iostream>
#include<string>
using namespace std;
class emp
{
protected:
string name,mail,adress;
int id,pn;
double bp;
public:
emp(string n,string m,string a,int i,int p,double b)
{
name=n;
mail=m;
adress=a;
id=i;
pn=p;
bp=b;
}

};
class room:public emp
{
public:
room(string n,string m,string a,int i,int p,double b):emp(n,m,a,i,p,b){
}
double da()
{
return 0.97*bp;
}
double hr()
{
return 0.10*bp;
}
double pf()
{
return 0.12*bp;
}
double club()
{
return 0.001 * bp;
}
double net()
{
double dedu=da()+hr()+pf()+club();
return bp-dedu;
}
void dis()
{
cout<<name<<endl;
cout<<mail<<endl;
cout<<id<<endl;
cout<<adress<<endl;
cout<<pn<<endl;
cout<<bp<<endl;
cout<<da()<<endl;
cout<<hr()<<endl;
cout<<pf()<<endl;
cout<<club()<<endl;
cout<<net()<<endl;
}
};
int main()
{
string name,mail,adress;
int id,pn;
double bp;
cin>>name>>mail>>adress>>id>>pn>>bp;
room e1(name,mail,adress,id,pn,bp);
e1.dis();
}

For 10:
#include<iostream>
#include<string>
using namespace std;
class emp
{
private:
int age,pn;
string name,add;
double bp;
public:
emp(int a,int p,string n,string ad,double b)
{
age=a;
name=n;
bp=b;
pn=p;
add=ad;
}
double da()
{
return 0.1*bp;
}
double hr()
{
return 0.1*bp;
}
double pf()
{
return 0.91*bp;
}
double g()
{
return bp+da()+hr();
}
double net()
{
return g()-pf();

}
void dis()
{
cout<<name<<endl;
cout<<pn<<endl;
cout<<age<<endl;
cout<<add<<endl;
cout<<bp<<endl;
cout<<da()<<endl;
cout<<hr()<<endl;
cout<<pf()<<endl;
cout<<g()<<endl;
cout<<net()<<endl;
}
};
int main()
{

int age[2],p[2];
string name[2],add[2];
double bp[2];
for(int i=0;i<2;i++){
cin>>name[i]>>p[i]>>age[i]>>add[i]>>bp[i];

}
emp e1[2] = {
emp(age[0], p[0], name[0], add[0], bp[0]),
emp(age[1], p[1], name[1], add[1], bp[1])
};
for (int i = 0; i < 2; ++i)
{
cout << "Details of employee " << i + 1 << ":" << endl;
e1[i].dis();
}
}

Fib class:

#include<iostream>
using namespace std;
class fib
{
private:
int n;
public:
void setl(int num)
{
n=num;
}
void a()
{
int a=0,b=1,c;
cout<<a<<endl;
cout<<b;
for(int i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
cout<<c;
}
}
};
int main()
{
int n;
cin>>n;
fib f;
f.setl(n);
f.a();
}

#include <iostream>
using namespace std;

class Rectangle {
private:
int width;
int height;

public:
// Constructor declaration
Rectangle(int w, int h);

// Member functions
int getArea();
int getPerimeter();
};

// Constructor definition outside the class


Rectangle::Rectangle(int w, int h) {
width = w;
height = h;
}
// Member function definitions
int Rectangle::getArea() {
return width * height;
}

int Rectangle::getPerimeter() {
return 2 * (width + height);
}

int main() {
// Create a Rectangle object
Rectangle rect(10, 5);

// Print the area and perimeter


cout << "Area: " << rect.getArea() << endl;
cout << "Perimeter: " << rect.getPerimeter() << endl;

return 0;
}

Multiple inhertience:
#include<iostream>
using namespace std;
class sum
{
protected:
int a;
public:
sum(int a1)
{
a=a1;

}
};
class sumb
{
protected:
int b;
public:
sumb(int b1)
{
b=b1;

}
};

class s:public sum,public sumb


{
public:
s(int a1,int b1):sum(a1),sumb(b1){
}
int su()
{
return a+b;
}
};
int main()
{
int a,b;
cin>>a>>b;
s s1(a,b);
cout<<s1.su();
}

Inhert:
#include<iostream>
using namespace std;
class shape
{
protected:
int a,r;
public:
shape(int a1,int r1)
{
a=a1;
r=r1;
}

};
class sq:public shape
{
public:
sq(int a1,int r1):shape(a1,r1){
}
int squ()
{
return a*a;
}
int c()
{
return 2*r*r;
}
};
int main()
{
int a,r;
cin>>a>>r;
sq s(a,r);
cout<<s.squ()<<endl;
cout<<s.c();
}

Pointer adress
#include <iostream>
using namespace std;
int main() {
int size = 5;
int arr[size] = {10, 20, 30, 40, 50};
for (int i = 0; i < size; ++i) {
cout << &arr[i] << endl;
}
}
Display using pointer
#include<iostream>
using namespace std;
class m
{
private:
int a,b;
public:
m(int a1,int b1)
{
a=a1;
b=b1;
}
void display()
{
cout<<a<<b<<endl;
}
};
int main()
{
int a,b;
cin>>a>>b;
m m1(a,b);
m*ptr=&m1;
ptr->display();
}

modellll
#include <iostream>
using namespace std;

// Rectangle class
class Rectangle {
private:
double length;
double width;

public:
// Parameterized constructor
Rectangle(double l, double w) {
length = l;
width = w;
}

// Method to calculate area


double calculateArea() {
return length * width;
}
};

int main() {
const int numRectangles = 3;
Rectangle rectangles[numRectangles] = {
Rectangle(4.5, 6.7),
Rectangle(2.0, 3.5),
Rectangle(5.0, 2.5)
};
// Calculate and display area of each rectangle
for (int i = 0; i < numRectangles; ++i) {
cout << "Area of Rectangle " << i+1 << ": " << rectangles[i].calculateArea() << endl;
}

return 0;
}

Max using pointers


#include<iostream>
using namespace std;
int main()
{
int a,b,c;
int *p1,*p2,*p3;
p1=&a;
p2=&b;
p3=&c;
cin>>a>>b>>c;
int max;
if(*p1>*p2&&*p1>*p3)
{
max=*p1;
}
if(*p2>*p1&&*p2>*p3)
{
max=*p2;
}
else
{
max=*p3;
}
cout<<max;

Display using pointers:


#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int *a=new int[n];
for(int i=0;i<n;i++)
{
cin>>*(a+i);
}
for(int i=0;i<n;i++)
{
cout<<*(a+i);
}
}

Skip
#include<iostream>
using namespace std;
class number {
protected:
int n, m, k;
public:
number(int nu, int mu, int ku) {
n = nu;
m = mu;
k = ku;
}
};

class skip: public number {


public:
skip(int nu, int mu, int ku): number(nu, mu, ku) {}

void b() {
for(int i=m;i<=n;i++) {
if (m == n) {

continue;
}
cout << m << endl;

}
}
};

int main() {
int n, m, k;
cin >> m >> n >> k;
skip n1(m, n, k);
n1.b();

return 0;
}

For 10:
int main()
{
const int to=2;
emp e1[to];
for(int i=0;i<to;i++){

int age,p;
string name,add;
double bp;
cin>>name>>p>>age>>add>>bp;
e1[i]=emp(age,p,name,add,bp);
}
for(int i=0;i<to;i++)
{
e1[i].dis();
}
}

You might also like