0% found this document useful (0 votes)
2 views

c++ code

The document contains multiple C++ code snippets demonstrating various programming concepts such as arrays, classes, composition, pointers, function overloading, friend functions, and polymorphism. It includes examples of printing arrays, using member initializers in classes, passing pointers by value and reference, and implementing polymorphic behavior with derived classes. Overall, it serves as a tutorial on fundamental C++ programming techniques.

Uploaded by

storybyman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c++ code

The document contains multiple C++ code snippets demonstrating various programming concepts such as arrays, classes, composition, pointers, function overloading, friend functions, and polymorphism. It includes examples of printing arrays, using member initializers in classes, passing pointers by value and reference, and implementing polymorphic behavior with derived classes. Overall, it serves as a tutorial on fundamental C++ programming techniques.

Uploaded by

storybyman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Print Arrays

#include <iostream>
using namespace std;

void printArray(int theArray[], int sizeOfArray);


int main() {
int bucky[3] = {12,3123,123};
int jessica[6] = {123,123,3412,4324,5435,4134};
printArray(bucky, 3);
printArray(jessica, 6);

void printArray(int theArray[], int sizeOfArray) {


for(int x = 0; x < sizeOfArray; x++){
cout << theArray[x] << endl;
}
}
\
// THe composition code it is basically how to use objects of one class to the
other class with member initializer (:)
// composition in the program so basically it means we are going to use
// objects of one class to another class through member initializers
#include <iostream>
#include <string>
using namespace std;

class Birthday{
public:
Birthday(int d, int m, int y){
day = d;
month = m;
year = y;
};
void printDate() {
cout << day << "/" << month << "/" << year << endl;
}
private:
int day;
int month;
int year;
};

class People {
public:
People(string x, Birthday bo)
: name(x), dateOfBirth(bo)
{

}
void printInfo(){
cout << name << " was born on " ;
dateOfBirth.printDate();
}
private:
string name;
Birthday dateOfBirth;

};
int main() {

Birthday bop(16,11,2000);
People po("Guruvansh", bop);
po.printInfo();

#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int x; x <=1;x++){

}
if(n/2==0){
return n*2;
}
if(n/2!=0){
return n*3 + 1;
}
else{
return 1;
}
return 0;
}

// pointer pass by value and pass by reference


// pointer pass by value and pass by reference
#include <iostream>
#define ll long long
using namespace std;

void printByValue(int x);


void printByReference(int *x);

int main() {

int betty =12 ;


int sandy = 13;
int *x;
x = &sandy;
cout << x ;
/*
printByValue(betty);
cout << betty << "\n";
printByReference(&sandy);
cout << sandy ;
*/
}

void printByValue(int x) {
x = 24343;
}
void printByReference(int *x) {
*x = 131312;
}

//sum of two numbers


#include <iostream>
using namespace std;

class Sally{
public:
void printCrap(int x, int y){
a = x;
b = y;
sum = a+b;
cout << sum;
}
private:
int a;
int b;
int sum;

};

int main() {
Sally sm;
sm.printCrap(23,54);

//friend function
// friend function
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

class StankFist{
public:
StankFist() {
stinkyVar = 0;
}
friend void stinkyFriend(StankFist &sfo);
private:
int stinkyVar;
};
void stinkyFriend(StankFist &sfo){
sfo.stinkyVar = 69;
cout << "changing value of class function through outside function by accessing
it through the friend function "<< sfo.stinkyVar << endl;
}
int main() {
StankFist jj;
stinkyFriend(jj);

}
////
Enemy ninja and monster
// program based on polymorphism :
#include <iostream>
using namespace std;

class Enemy{
public:
void setAttackPower(int x)
{
attackPower = x;
}
void attack() {
cout << "VOid attack by RegEnemy" << endl;
}
protected:
int attackPower;
};
class Ninja: public Enemy {
public:
void attack() {
cout << "NInja attack ! Ninja chop! Chop! chop! -" << attackPower
<< endl;
}
};
class Monster: public Enemy {
public:
void attack() {
cout << "Monster! Roarrr.... Monster will ear you! " << attackPower
<< endl;
}
};

class RegEnemy: public Enemy {


public:
void attack() {
cout << "Attack ,,, " << attackPower << endl;
}
};

int main() {
Ninja n;
Monster m;
Enemy *en = &n;
Enemy *em = &m;
en->setAttackPower(360);
em->setAttackPower(888);
n.attack();
m.attack();
RegEnemy re;
Enemy *eo = &re;
re.attack();

return 0;
}

You might also like