SOMETHING ABOUT C++
Contents:-
data types
Identifiers
Keywords
calculate the no.of vowels
fectorial of a number
Armstrong number
C++ and oop
class,object,constructor,copy constructor,
destructor and more programmings
DATA TYPES:-
char
int
float
double
void
bool
DATA TYPE MODIFIERS:-
signed
unsigned
long
short
Identifiers
hello_world
_abc
DEFINED
Reserved Words(Keywords)
Variable Declaration Words:-
char
double
float
Statements Words:-
break
else
if
case
Storage Allocation Identifier
const
volatile
static
void
…………………………………
A program in c++ to calculate the number of vowels .
#include<iostream>
using namespace std;
int main()
{
int a=0,e=0,i=0,o=0,u=0;
int i;
char ch[100];
cout<<"This program read only 20 character.";
cout<<"\nEnter Only in capital letter:";
cin>>ch;
cout<<"\nA E I O U\n";
for(i=0;i<=19;i++)
{
switch(ch[i])
{
case 'A':
a=a+1;
break;
case 'E':
e=e+1;
break;
case 'I':
i=i+1;
break;
case 'O':
o=o+1;
break;
case 'U':
u=u+1;
break;
}
}
cout<<a<<" "<<e<<" "<<i<<" "<<o<<" "<<u;
return 0;
}
Output:
This program read only 20 character.
Enter only in capital letter:
AAAPQEIGHDHJJFDDFHJJDF
A E I O U
3 1 1 0 0
…………………………………………………………
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter a no.";
cin>>a;
cout<<"Entered no. is "<<a;
return 0;
}
Output:
Enter a no.3
Entered no. is 3
-------------------------
use of 'endl'
…………………………………
#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome"<<endl;
cout<<"c++ programming";
cout<<"It's a test.";
return 0;
}
Output:
Welcome
c++programmingIt's a test.
…………………………………………………
…………………………………………………
class and members
#include<iostream>
using namespace std;
class student
{
public:
void display()
{
cout<<"Welcome";
}
};
int main()
{
student a;
a.display();
return 0;
}
Output:
Welcome
…………………………………
Other method to write this program
…………………………………
#include<iostream>
using namespace std;
class student
{
public:
void display();
};
void student::display()
{
cout<<"Welcome";
}
int main()
{
student a;
a.display();
return 0;
}
Output:
Welcome
………………………………
………………………………
#include<iostream>
using namespace std;
class school
{
public:
int schoolno;
void enter();
void display();
};
void school::enter()
{
cout<<"Enter school number:";
cin>>schoolno;
}
void school::display()
{
cout<<"School number is "<<schoolno;
}
int main()
{
school abcd;
abcd.enter();
abcd.display();
return 0;
}
Output:
Enter school number:3
School number is 3
…………………………………………………
Constructors
#include<iostream>
using namespace std;
class school
{
public:
int schoolid;
school(); /* constructor */
void enter();
void display();
};
school::school() //constructor
{
cout<<"Welcome\n";
}
void school::enter()
{
cout<<"Enter school id:";
cin>>schoolid;
}
void school::display()
{
cout<<"School id:"<<schoolid;
}
int main()
{
school sss;
/*Constructors enable automatic initialisation of objects */
sss.enter();
sss.display();
return 0;
}
Output:
Welcome
Enter school id:4
School id:4
COPY CONSTRUCTOR
#include<iostream>
using namespace std;
class school
{
public:
string name;
school();
school(school &cpy)
void display();
};
school::school()
{
name="Government primary school";
}
//copy constructor
school::school(school &cpy)
{
name=cpy.name;
}
void school::display()
{
cout<<name;
}
int main()
{
school obj;
obj.display();
return 0;
}
Output:
Government primary school
…………………………………………………
DESTRUCTORS
#include<iostream>
using namespace std;
class test
{
public:
~test();
};
test::~test()
{
cout<<"Welcome";
}
int main()
{
test a;
return 0;
}
Output:
Welcome
------------------------
#include<iostream>
using namespace std;
class test
{
public:
~test()
{
cout<<"Welcome";
}
};
int main()
{
test a;
return 0;
}
Output:
Welcome
------------------------------------
Both constructor and destructor
#include<iostream>
using namespace std;
class test
{
public:
test()
{
cout<<"Welcome\nEnter a number:";
}
~test()
{
cout<<\nIt's over.";
}
void even()
{
cout<<"The number is even.";
}
void odd()
{
cout<<"The number is odd.";
}
};
int main()
{
int a;
test aaa;
cin>>a;
if(a%2==0)
{
aaa.even();
}
else
{
aaa.odd();
}
return 0;
}
Output:
Enter a number:4
The number is even.
It's over.
…………………………………………………
------------------------------------
A program in c++ to find the factorial of a number.
(Using class and objects)
#include<iostream>
using namespace std;
class factorial
{
public:
int n,sum=1;
void calculation();
void display();
};
void factorial::calculation()
{
int i;
cout<<"Enter the number:";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum*i;
}
}
void factorial::display()
{
cout<<"Factorial of the given number="<<sum;
}
int main()
{
factorial aaa;
aaa.calculation();
aaa.display();
return 0;
}
Output:
Enter the number:4
Factorial of the given number=24
………………………………………………………
A program to find Armstrong number.
#include<iostream>
using namespace std;
int main()
{
int n,r;
int sum=0;
int temp;
cout<>"Enter a number:";
cin>>n;
temp=n;
while(temp != 0)
{
r=temp%10;
sum=sum+r*r*r;
temp=temp/10;
}
if(sum==n)
{
cout<<"Entered number is Armstrong number.";
}
else
{
cout<<"Entered number is not Armstrong number.";
}
return 0;
}
Enter a number:453
Entered number is not Armstrong number.
CLICK FOR SEARCH TOPIC
Please post comment in comment box.Also you can send question.
Contents:-
data types
Identifiers
Keywords
calculate the no.of vowels
fectorial of a number
Armstrong number
C++ and oop
class,object,constructor,copy constructor,
destructor and more programmings
DATA TYPES:-
char
int
float
double
void
bool
DATA TYPE MODIFIERS:-
signed
unsigned
long
short
Identifiers
hello_world
_abc
DEFINED
Reserved Words(Keywords)
Variable Declaration Words:-
char
double
float
Statements Words:-
break
else
if
case
Storage Allocation Identifier
const
volatile
static
void
…………………………………
A program in c++ to calculate the number of vowels .
#include<iostream>
using namespace std;
int main()
{
int a=0,e=0,i=0,o=0,u=0;
int i;
char ch[100];
cout<<"This program read only 20 character.";
cout<<"\nEnter Only in capital letter:";
cin>>ch;
cout<<"\nA E I O U\n";
for(i=0;i<=19;i++)
{
switch(ch[i])
{
case 'A':
a=a+1;
break;
case 'E':
e=e+1;
break;
case 'I':
i=i+1;
break;
case 'O':
o=o+1;
break;
case 'U':
u=u+1;
break;
}
}
cout<<a<<" "<<e<<" "<<i<<" "<<o<<" "<<u;
return 0;
}
Output:
This program read only 20 character.
Enter only in capital letter:
AAAPQEIGHDHJJFDDFHJJDF
A E I O U
3 1 1 0 0
…………………………………………………………
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter a no.";
cin>>a;
cout<<"Entered no. is "<<a;
return 0;
}
Output:
Enter a no.3
Entered no. is 3
-------------------------
use of 'endl'
…………………………………
#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome"<<endl;
cout<<"c++ programming";
cout<<"It's a test.";
return 0;
}
Output:
Welcome
c++programmingIt's a test.
…………………………………………………
…………………………………………………
class and members
#include<iostream>
using namespace std;
class student
{
public:
void display()
{
cout<<"Welcome";
}
};
int main()
{
student a;
a.display();
return 0;
}
Output:
Welcome
…………………………………
Other method to write this program
…………………………………
#include<iostream>
using namespace std;
class student
{
public:
void display();
};
void student::display()
{
cout<<"Welcome";
}
int main()
{
student a;
a.display();
return 0;
}
Output:
Welcome
………………………………
………………………………
#include<iostream>
using namespace std;
class school
{
public:
int schoolno;
void enter();
void display();
};
void school::enter()
{
cout<<"Enter school number:";
cin>>schoolno;
}
void school::display()
{
cout<<"School number is "<<schoolno;
}
int main()
{
school abcd;
abcd.enter();
abcd.display();
return 0;
}
Output:
Enter school number:3
School number is 3
…………………………………………………
Constructors
#include<iostream>
using namespace std;
class school
{
public:
int schoolid;
school(); /* constructor */
void enter();
void display();
};
school::school() //constructor
{
cout<<"Welcome\n";
}
void school::enter()
{
cout<<"Enter school id:";
cin>>schoolid;
}
void school::display()
{
cout<<"School id:"<<schoolid;
}
int main()
{
school sss;
/*Constructors enable automatic initialisation of objects */
sss.enter();
sss.display();
return 0;
}
Output:
Welcome
Enter school id:4
School id:4
COPY CONSTRUCTOR
#include<iostream>
using namespace std;
class school
{
public:
string name;
school();
school(school &cpy)
void display();
};
school::school()
{
name="Government primary school";
}
//copy constructor
school::school(school &cpy)
{
name=cpy.name;
}
void school::display()
{
cout<<name;
}
int main()
{
school obj;
obj.display();
return 0;
}
Output:
Government primary school
…………………………………………………
DESTRUCTORS
#include<iostream>
using namespace std;
class test
{
public:
~test();
};
test::~test()
{
cout<<"Welcome";
}
int main()
{
test a;
return 0;
}
Output:
Welcome
------------------------
#include<iostream>
using namespace std;
class test
{
public:
~test()
{
cout<<"Welcome";
}
};
int main()
{
test a;
return 0;
}
Output:
Welcome
------------------------------------
Both constructor and destructor
#include<iostream>
using namespace std;
class test
{
public:
test()
{
cout<<"Welcome\nEnter a number:";
}
~test()
{
cout<<\nIt's over.";
}
void even()
{
cout<<"The number is even.";
}
void odd()
{
cout<<"The number is odd.";
}
};
int main()
{
int a;
test aaa;
cin>>a;
if(a%2==0)
{
aaa.even();
}
else
{
aaa.odd();
}
return 0;
}
Output:
Enter a number:4
The number is even.
It's over.
…………………………………………………
------------------------------------
A program in c++ to find the factorial of a number.
(Using class and objects)
#include<iostream>
using namespace std;
class factorial
{
public:
int n,sum=1;
void calculation();
void display();
};
void factorial::calculation()
{
int i;
cout<<"Enter the number:";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum*i;
}
}
void factorial::display()
{
cout<<"Factorial of the given number="<<sum;
}
int main()
{
factorial aaa;
aaa.calculation();
aaa.display();
return 0;
}
Output:
Enter the number:4
Factorial of the given number=24
………………………………………………………
A program to find Armstrong number.
#include<iostream>
using namespace std;
int main()
{
int n,r;
int sum=0;
int temp;
cout<>"Enter a number:";
cin>>n;
temp=n;
while(temp != 0)
{
r=temp%10;
sum=sum+r*r*r;
temp=temp/10;
}
if(sum==n)
{
cout<<"Entered number is Armstrong number.";
}
else
{
cout<<"Entered number is not Armstrong number.";
}
return 0;
}
Enter a number:453
Entered number is not Armstrong number.
CLICK FOR SEARCH TOPIC
Please post comment in comment box.Also you can send question.


No comments:
Post a Comment