c++ is a high level programming language developed bjerne stroushup in year 1979 at bell lab.c++ is object oriented programming language.
c++ case-sensitive language.
in earlier the name of c++ was "c with classes"
c++ is a protable programming language.
syntax of c++ program:
#include<iostream> // header file
using namespace std;
return type main(){ // main program start from here
//code
} //end of main program
input device is used to give a command to the computer. cin for input. cout for output
# is a preprocessor directive.
include is a statement
header file: the file that tell compiler how to call some functionality without knowing how the functionality actually work is called header file.
* compilation and execution process of c++
program: first.cpp
preprocessor //link header file
compiler
object file
linker
executable file
loader
program run
* datatypes: datatype is a programming language is the collection of data having fixed meaning as well as char,some of them are int,char float,double short.
datatype defines the type of value means what kind of value the variable will store
datatype are used to
identify the type of variable when it is declared
identify the type of return value of the function.
identify the type of parameter expected by the function.
type of datatype
* basic datatype: these are fundamental data type namily int,float char
int,float : 4 byte
char : 1 byte
double : 8 byte
bool : 1 byte
void: no size
* derived datatype: Array,function,pointer,reference
* userdefined datatype: structure,union,class,enumeration
variable : a variable is defined as the meaning of name given to a data storage location in computer memory.the name of memory location where we store value.
- variable are case-sensitive.
- variable can start with underscore.
- we can't give extra spaces between the variable.
num_m = 10
* type of variable: global, local,static variable.
#include<iostream>
using namespace std;
int b = 20; //global
static int d = 40; //static variable
int main(){
int a = 10; //local
static int c = 30;
cout <<b <<a << c << d;
return 0;
}
In C++, the length of an array can be determined using the sizeof operator. This operator can be used to find the total size of the array in bytes and the size of a single element, which can then be used to calculate the number of elements in the array.
#include <iostream>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int arraySize = sizeof(arr) / sizeof(arr[0]);
std::cout << "The length of the array is: " << arraySize << std::endl;
return 0;
}
switch statement in c++
#include <iostream>
using namespace std;
int main() {
int day;
cout << "Enter day: ";
cin >> day;
switch(day) {
case 1:
cout << "Sunday";
break;
case 2:
cout << "Monday";
break;
case 3:
cout << "Tuesday"; // Changed to Tuesday
break;
case 4:
cout << "Wednesday";
break;
case 5:
cout << "Thursday";
break;
case 6:
cout << "Friday";
break;
case 7:
cout << "Saturday";
break;
default:
cout << "Invalid day";
}
return 0;
}
student grade using c++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string subject[5] = {"english", "math", "physics", "c++", "oop programming"};
int marks[5];
int total;
float per;
// Input marks for each subject
for (int i = 0; i < 5; i++) {
cout << "Enter the marks of " << subject[i] << ": ";
cin >> marks[i];
}
// Print the header
cout << "Subject" <<"Marks" << endl;
// Print marks for each subject
for (int i = 0; i < 5; i++) {
cout <<subject[i] << marks[i] << endl;
total = total + marks[i];
per = (total / 5);
}
cout <<"Total " << total << endl;
cout <<"Percentage "<< per << endl;
return 0;
}
using class and object
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class student{
public:
string subject[5] = {"english", "math", "physics", "c++", "oop programming"};
int marks[5];
int total;
float per;
void Enterdata(){
// Input marks for each subject
for (int i = 0; i < 5; i++) {
cout << "Enter the marks of " << subject[i] << ": ";
cin >> marks[i];
}
}
void showdata(){
cout << "Subject" <<"Marks" << endl;
// Print marks for each subject
for (int i = 0; i < 5; i++) {
cout <<subject[i] << marks[i] << endl;
total = total + marks[i];
per = (total / 5);
}
}
};
int main() {
student s1;
s1.Enterdata();
s1.showdata();
cout <<"Total " << s1.total << endl;
cout <<"Percentage "<< s1.per << endl;
return 0;
}
fibonacci series from 1 and 100
for eg : 0,1,1,2,3,5,8,13...
n1,n2,n3
n1 = 0, n2 = 1, n3 = n1 + n2,n1=n2,n2=n3
#include <iostream>
using namespace std;
int count = 0;
int main() {
int n1 = 1;
int n2 = 1;
while(n2 < 100){
int n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout <<n2<<endl;
}
return 0;
}
calculate reverse of a number
#include <iostream>
using namespace std;
int main() {
int n = 345;
int temp,rev,rem;
while(n != 0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
cout <<rev;
return 0;
}
Palindrome program in c++
#include <iostream>
using namespace std;
int main() {
int n = 3333;
int temp,rev,rem;
temp = n;
while(n != 0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if(temp == rev){
cout <<"palindrom number";
}else{
cout <<"Not palindrom number";
}
return 0;
}
Check number is prime or not:
number divide by 1 and itself is prime
#include <iostream>
using namespace std;
int main() {
int n = 5;
int count = 0;
for(int i=1; i <= n; i++){
if(n % i == 0){
count++;
}
}
if(count == 2){
cout <<"Prime number";
}else{
cout <<"Not a prime number";
}
return 0;
}
pointer to the function
#include <iostream>
using namespace std;
void show(int *p) {
cout << "value of a is " << *p << endl;
cout << "address of a is " << p << endl;
}
int main() {
int a = 10;
show(&a);
return 0;
}
find largest number using
#include <iostream>
using namespace std;
void largest(int *a,int *b){
int large;
if(*a > *b){
large = *a;
}else{
large = *b;
}
cout <<"large: "<<large;
}
int main() {
int a = 10,b = 12;
largest(&a,&b);
return 0;
}
pointer to Array
#include <iostream>
using namespace std;
int main() {
int student[2] = {1,2};
int *p; // *p contain value
p = &student[0]; // it contain address of the element
cout <<p;
return 0;
}
int student[2] = {1,2};
int*p;
p = student;
cout <<p[0];
Macros
#include <iostream>
using namespace std;
#define PI 3.14
#define show(a,b) a+b
int main() {
float a;
int r = 3;
a = PI * r * r;
cout <<"area: "<<a;
cout <<show(3,6);
return 0;
}
inline function in c++
#include <iostream>
using namespace std;
inline void sum(int a,int b){
int s;
s = a + b;
cout <<"sum : "<<s;
}
int main() {
sum(3,4);
return 0;
}
function overloading
#include <iostream>
using namespace std;
void sum(int a,int b){
int s;
s = a + b;
cout <<"sum : "<<s;
}
void sum(int a,int b,int c){
int s;
s = a + b + c;
cout <<"sum : "<<s;
}
int main() {
sum(3,4);
sum(3,4,5);
return 0;
}
pass by value : assigning value to the variable.
int a=2,b;
b = a;
pass by reference : int &a,b=5;
&a = b;
friend function in c++
#include <iostream>
using namespace std;
class student{
void showname(){
cout <<"krishna kharal";
}
friend void show(student &s);
};
void show(student &s){
s.showname();
}
int main() {
student s;
show(s);
return 0;
}
class and oop and other oop concept
#include <iostream>
using namespace std;
class student {
public:
string name;
int age;
student(string n, int a) {
name = n;
age = a;
}
void getdata() {
cout << "Enter your name and age: ";
cin >> name >> age;
}
void displaydata() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
friend void showname(student s);
void showtext();
};
// Member function of the class student
void student::showtext() {
cout << "This is a member function of the student class" << endl;
}
void showname(student s) {
cout << "Name: " << s.name << ", Age: " << s.age << endl;
}
class teacher : public student {
public:
double salary;
// Constructor of teacher class that inherits name and age from base class
teacher(string n, int a, double s) : student(n, a), salary(s) {}
void displayteacherdata() {
displaydata(); // Calling the base class function
cout << "Salary: " << salary << endl;
}
};
int main() {
teacher t1("Krishna Kharal", 20, 343.0);
cout << "Teacher Information:" << endl;
t1.displayteacherdata();
t1.showtext();
// Using the friend function to display name and age
showname(t1);
return 0;
}
Comments
Post a Comment