C++ Codes basic to advance
784 subscribers
4 photos
2 videos
49 links
Download Telegram
// Example of Multiple Inheritance
#include <iostream>
using namespace std;

class Base1 {
public:
void function1() {
cout << "This is function1 of Base1." << endl;
}
};

class Base2 {
public:
void function2() {
cout << "This is function2 of Base2." << endl;
}
};

class Derived: public Base1, public Base2 {
public:
void function3() {
cout << "This is function3 of Derived." << endl;
}
};

int main () {
Derived derived;
derived.function1();
derived.function2();
derived.function3();
return 0;
} // @CPP_Coding
#include <iostream>
using namespace std;

class Balls {
private:
    int num_balls = 0;
public:
    void insert() {
        num_balls++;
    }

    void remove() {
        if (num_balls > 0) {
            num_balls--;
        } else {
            cout << "Error: Cannot remove ball, no balls left." << endl;
        }
    }

    void show() {
        cout << "Number of balls: " << num_balls << std::endl;
    }
};


// Main function to use that class
int main() {
    Balls my_balls;
int n;

for(int i = 0; i < 2; i++){
cout<< "\nHow many balls wanna insert: ";
cin >> n;
for(int j = 0; j < n; j++)
    my_balls.insert();
    my_balls.show();

cout<< "\nHow many balls wanna remove: ";
cin >> n;
for(int j = 0; j < n; j++)
    my_balls.remove();
    my_balls.show();
}
    return 0;
}
/* Answer 👆
Task:👇

Make a class of balls in that class there should be functions
Of inserting balls
removing balls
And showing balls

@CPP_Coding */
👍3
All Codes
@C_Code5
@CPP_Coding
@Python_Codes_Pro
@Java_Codes_Pro
@jsCode0

Diffrent free Compiler bots
For group @CodeCompiler_Bot
For channel @IOChannel_bot
For Channel Cmpl @Compiler0bot
For inline @cmpbbot

info in @LogicBots

Discussion
@bca_mca_btech
// basic c++ program
#include <iostream>

int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
// @CPP_Coding
// Addition of two numbers
#include <iostream>

int main() {
int num1, num2, sum;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
sum = num1 + num2;
std::cout << "Sum: " << sum << std::endl;
return 0;
} // see t.me/CPP_Coding/46
// factorial
#include <iostream>

int main() {
int n;
unsigned long long factorial = 1;
std::cout << "Enter a positive integer: ";
std::cin >> n;

for (int i = 1; i <= n; ++i) {
factorial *= i;
}

std::cout << "Factorial of " << n << " = " << factorial << std::endl;
return 0;
} // see t.me/CPP_Coding/46
// Fibonacci series

#include <iostream>

int main() {
int n, num1 = 0, num2 = 1, nthTerm;
std::cout << "Enter the number of terms: ";
std::cin >> n;

std::cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
std::cout << num1 << " ";
nthTerm = num1 + num2;
num1 = num2;
num2 = nthTerm;
}

return 0;
} // see t.me/CPP_Coding/46
// String functions in c++ part 1

#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, World!";

// length()
int length = str.length();
cout << "Length of the string: " << length << endl;

// substr()
string substring = str.substr(0, 5);
cout << "Substring: " << substring << endl;

// find()
int index = str.find("World");
cout << "'World' found at index: " << index << endl;

// compare()
string str2 = "Hello, World!";
int compareResult = str.compare(str2);
if(compareResult == 0)
cout << "Strings are equal." << endl;
else
cout << "Strings are not equal." << endl;

// append()
string appendedString = str.append(" Welcome!");
cout << "Appended string: " << appendedString << endl;

// replace()
str.replace(7, 5, "Universe");
cout << "Replaced string: " << str << endl;

// erase()
str.erase(0, 7);
cout << "Erased string: " << str << endl;

// insert()
str.insert(0, "Hello, ");
cout << "Inserted string: " << str << endl;

// find_first_of()
int firstIndex = str.find_first_of("lo");
cout << "First occurrence of 'lo' at index: " << firstIndex << endl;

// find_last_of()
int lastIndex = str.find_last_of("lo");
cout << "Last occurrence of 'lo' at index: " << lastIndex << endl;

return 0;
}
👍1
//String functions in c++ part 2

#include <iostream>
#include <string>
using namespace std;

int main() {
string str = "Hello, World!";

// upper_case()
for(int i=0; i<str.length(); i++)
str[i] = toupper(str[i]);
cout << "Uppercase string: " << str << endl;

// lower_case()
for(int i=0; i<str.length(); i++)
str[i] = tolower(str[i]);
cout << "Lowercase string: " << str << endl;

// empty()
bool isEmpty = str.empty();
cout << "Is string empty? " << (isEmpty ? "Yes" : "No") << endl;

// front()
char frontChar = str.front();
cout << "First character: " << frontChar << endl;

// back()
char backChar = str.back();
cout << "Last character: " << backChar << endl;

// c_str()
const char* cString = str.c_str();
cout << "C-style string: " << cString << endl;

// stoi()
string numericString = "12345";
int numericValue = stoi(numericString);
cout << "Numeric value: " << numericValue << endl;

// to_string()
int intValue = 98765;
string strValue = to_string(intValue);
cout << "String value: " << strValue << endl;

// resize()
str.resize(5);
cout << "Resized string: " << str << endl;

// clear()
str.clear();
cout << "Cleared string: " << str << endl;

return 0;
} // see t.me/CPP_Coding/46
In upper 2 programs i given 20 string functions 10 10 on each
Learn it

You can also run it by clicking on run button then show button
[Comment here for anything else]
// Function declaration in c++
#include <iostream>

void greet() {
std::cout << "Hello, World!" << std::endl;
}

int main() {
greet(); // Output: Hello, World!
return 0;
}
// @CPP_Coding
// Function with parameters and return value:

#include <iostream>

int add(int a, int b) {
return a + b;
}

int main() {
int result = add(3, 4);
std::cout << "The sum is: " << result << std::endl;
return 0;
}

// @CPP_Coding
// Function with default parameter values:

#include <iostream>

void printMessage(std::string message = "Hello, World!") {
std::cout << message << std::endl;
}


int main() {

printMessage();
printMessage("Greetings!");

return 0;
}

// @CPP_Coding
// Function with function overloading:

#include <iostream>

int multiply(int a, int b) {
    return a * b;
}

double multiply(double a, double b) {
    return a * b;
}

int main() {
    int result1 = multiply(3, 4);
    double result2 = multiply(2.5, 1.5);

    std::cout << "Result 1: " << result1 << std::endl;

    std::cout << "Result 2: " << result2 << std::endl;
   
     return 0;
} // @CPP_Coding
// taking inputs by cpp

#include <iostream>
#include <string>

int main() {
std::string name;

std::cout << "Enter your first name: ";
std::cin >> name;

std::cout << "Hello, " << name << "! Nice to meet you!" << std::endl;

return 0;
}
// @CPP_Coding
👍2
// taking string ( with spaces )

#include <iostream>
#include <string>

int main() {
std::string name;

std::cout << "Enter your name: ";
std::getline(std::cin, name);

std::cout << "Hello, " << name << "! Nice to meet you!" << std::endl;

return 0;
}
// @CPP_Coding
// Taking inputs ( integers in cpp )

#include <iostream>
#include <string>

int main() {
std::string num1, num2;

std::cout << "Enter first number: ";
std::cin >> num1;

std::cout << "Enter second number: ";
std::cin >> num2;

// Typecasting string to integer
int num1Int = std::stoi(num1);
int num2Int = std::stoi(num2);

std::cout << "Sum of numbers is " << num1Int + num2Int << std::endl;

return 0;
}
// @CPP_Coding
Practice your own codes in
@IO_Coding

Ask doubts in
@bca_mca_btech

Comment here if any question
//To check numbers Amicable or not..
//Definition of Amicable nums:👉Amicable numbers are two different positive integers such that each number is the sum of the proper divisors of the other number...😊

#include<iostream>
using namespace std;
int s_divisors (int N) {
int i,s=0;
for(i=1; i<=N/2; i++) {
if(N%i==0)
s=s+i;
}
return s;
}
int main() {
int a,b;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
if((s_divisors(a)==b)&&(s_divisors(b)==a))
cout<<a<<" and "<<b<<" are Amicable✔️";
else
cout<<a<<" and "<<b<<" are Not Amicable";
return 0;
}
//@CPP_Coding
👍3
//To find Largest word and Smallest word in a string..
#include<iostream>
#include<string.h>
using namespace std;
int main() {
    char str[100]= {0},substr[100][100]= {0};
    cout<<"Enter string:";
    cin.getline (str, 100);
    int i = 0, j = 0, k = 0, a, minIndex = 0, maxIndex = 0, max = 0, min = 0;
    while (str[k] != '\0') {
        j = 0;
        while (str[k] != ' ' && str[k] != '\0' && str[k]!='.') {
            substr[i][j] = str[k];
            k++;
            j++;
        }
        substr[i][j] = '\0';
        i++;
        if (str[k] != '\0') {
            k++;
        }
    }
    int len = i;
    max = strlen(substr[0]);
    min = strlen(substr[0]);

    for (i = 0; i < len; i++) {
        a = strlen(substr[i]);
        if (a > max) {
            max = a;
            maxIndex = i;
        }
        if (a < min) {
            min = a;
            minIndex = i;
        }
    }
    cout<<"Largest Word is:" <<substr[maxIndex]<<endl<<"Smallest word is:"<<substr[minIndex]<<endl;
    return 0;
}
//@CPP_Coding
👍1
//Convert string to Title Case🌚
#include<iostream>
using namespace std;
void titleCase(char str[]) {
    int x=0;
    int prevChar=' ';
    while(str[x]!='\0') {
        if (prevChar==' ' or prevChar=='\t' or prevChar=='\n') {
            if(str[x]>='a'&& str[x]<='z')
                str[x]=str[x]-32;
        }
        else if(str[x]>='A'&& str[x]<='Z')
            str[x]=str[x]+32;

        prevChar=str[x];
        x++;
    }
}
int main() {
    char str[50];
    cout<<"Enter a string:";
    cin.getline(str,50);

    titleCase(str);
    cout<<str;
    return 0;
}

//@CPP_CODES_PRO
👍2