Phone: +255 692 127 931

Thursday, July 30, 2020

C++ Programming Language

C++ PROGRAMMING

1.     Introduction

2.     Windows Installation(IDE)

3.     Mac Installation

4.     Setup & Hello World

#include <iostream>

 

using namespace std;

 

int main()

{

    cout<<"Hello world"<<endl;

    cout<<"Am not human being"<<endl;

    return 0;

}

5.     Drawing a Shape

   cout<<"    /|"<<endl;

    cout<<"   / |"<<endl;

    cout<<"  /  |"<<endl;

    cout<<" /   |"<<endl;

    cout<<"/__|"<<endl;

6.     Variables

#include <iostream>

 

using namespace std;

 

int main()

{

    string characterName="Ima";

    int characterInt=90;

 

    cout<<"Once there was man called "<<characterName<<endl;

    cout<<"He was "<<characterInt<<" years old"<<endl;

    cout<<"He real like the name "<<characterName<<endl;

    cout<<"But he did not like the being "<<characterInt<<endl;

 

    return 0;

}

 

7.     Data Types

   int age=77;

    string name="Njohole";

    char letter='A';

    float pie=3.14;

    double bigPie=54332.8932;

    bool isTrue=false;

 

    cout<<age<<endl;

    cout<<name<<endl;

    cout<<letter<<endl;

    cout<<pie<<endl;

    cout<<bigPie<<endl;

    cout<<isTrue<<endl;

8.     Working With Strings

#include <iostream>

 

using namespace std;

 

int main()

{

    string phrase="THE CITIZEN COLLEGE";

    phrase[5]='P';

    //string subphrase;

    //subphrase=phrase.substr(7,3);

    //cout<<subphrase;

    cout<<phrase;

 

    return 0;

}

 

9.     Working With Numbers

#include <iostream>

#include<cmath>

 

using namespace std;

 

int main()

{

    /*

     cout<<5+7<<endl;

     cout<<5*7<<endl;

     cout<<5-7<<endl;

     cout<<5/7<<endl;

     cout<<5%7<<endl;

    */

    cout<<fmax(43,89)<<endl;

     cout<<fmin(43,89)<<endl;

    cout<<ceil(789.43212)<<endl;

    cout<<floor(789.43212)<<endl;

    cout<<round(43982.87328)<<endl;

    cout<<sqrt(49)<<endl;

 

 

    return 0;

}

10.      Getting User Input

#include <iostream>

 

using namespace std;

 

int main()

{

   /* int height;

    cout<<"Enter your height: ";

    cin>>height;

 

    cout<<"Your height is: "<<height<<endl;

    */

 

    string name;

    cout<<"Enter your name:";

    getline(cin,name);

 

    cout<<"Your name is: "<<name<<endl;

 

 

    return 0;

}

11.      Building a Calculator

#include <iostream>

 

using namespace std;

 

int main()

{

   int num1,num2;

   cout<<"Enter first number: ";

   cin>>num1;

 

    cout<<"Enter second number: ";

    cin>>num2;

 

       int add;

       add=num1+num2;

 

   cout<<"Sum is: "<<add<<endl;

 

    int sub;

       sub=num1-num2;

 

   cout<<"Sub is: "<<sub;

 

 

    return 0;

}

12.      Building a Mad Libs

#include <iostream>

 

using namespace std;

 

int main()

{

    string color, pluralNoun, celebrity;

    cout<<"Enter color you liking: ";

    getline(cin,color);

 

    cout<<"Enter pluralNoun: ";

    getline(cin, pluralNoun);

 

    cout<<"Enter your celebrity: ";

    getline(cin, celebrity);

 

 

    cout<<"Rose are "<<color<<endl;

    cout<<pluralNoun<<" are blue"<<endl;

    cout<<"I love "<<celebrity<<endl;

 

    return 0;

}

 

13.      Arrays

            /*

    int randNum[5]={44,65,21,76,90};

 

    randNum[2]=70;

 

    cout<<randNum[2];

 

 

    int i=0;

    while(i<5){

        cout<<randNum[i]<<endl;

        i++;

    }

 

 

    for(int i=0; i<5; i++){

        cout<<randNum[i]<<endl;

    }

14.      Functions

            #include <iostream>

 

using namespace std;

/*

void name(){

        cout<<"We are in function"<<endl;

}

*/

 

int add(int num1, int num2){

    int ans;

         ans=num1+num2;

 

    return ans;

}

int main()

{

    int a,b;

    cout<<"Enter first number: ";

    cin>>a;

 

    cout<<"Enter second number: ";

    cin>>b;

 

    cout<<a<< " + "<<b<<" = "<<add(a,b);

 

    return 0;

}

15.      Return Statement

            #include<iostream>

 

using namespace std;

 

int prod(int a){

 

    return a*a;

 

}

 

int main()

{

 

    cout<<"The product is "<<prod(9);

 

    return 0;

}

 

 

16.      If Statements

#include <iostream>

 

using namespace std;

 

int main()

{

    bool isMale=true;

    bool isTall=false;

 

    if((isMale)|| (isTall)){

        cout<<"You are a male and Tall"<<endl;

    }

    else{

        cout<<"You are tall";

    }

 

    return 0;

}

17.      If Statements (con't)

   #include <iostream>

 

using namespace std;

 

int main()

{

    int a,b,c;

        a=6;

        b=8;

        c=3;

 

    if((a>b)&& (a>c)){

        cout<<a;

    }

    else if((b>a)&& (b>c))

    {

            cout<<b;

     }

    else{

        cout<<c;

 

    }

 

    return 0;

}

 

18.      Building a Better Calculator

 

#include <iostream>

 

using namespace std;

 

int main()

{

    int num1, num2;

 

    cout<<"Enter first number:";

    cin>>num1;

 

    char op;

    cout<<"Choose your option(+,-,/,*):";

    cin>>op;

 

    cout<<"Enter the second number:";

    cin>>num2;

 

    int result;

 

 

    if(op=='+'){

        cout<<num1+num2;

    }

    else if(op=='-'){

         cout<<num1-num2;

    }

    else if(op=='*'){

        cout<<num1*num2;

    }

    else if(op=='/'){

         cout<<num1/num2;

    }

    else{

        cout<<"Invalid operator:";

    }

    return 0;

}

 

 

 

19.      Switch Statements

#include <iostream>

 

using namespace std;

 

int main()

{

    int mark;

    cout<<"Enter your mark: ";

    cin>>mark;

 

    switch(mark){

    case 90:

        cout<<"A";

        break;

    case 80:

        cout<<"B";

        break;

    case 60:

        cout<<"C";

        break;

    case 40:

        cout<<"D";

        break;

    case 30:

        cout<<"F";

        break;

    default:

        cout<<"Invalid input";

        break;

 

    }

 

    return 0;

}

20.      While Loops

   #include <iostream>

 

using namespace std;

 

int main()

{

     int i=0;

        while(i<20){

            cout<<"Hi world"<<endl;

            i++;

        }

 

    return 0;

}

21.      Building a Guessing Game

#include <iostream>

 

using namespace std;

 

int main()

{

   int secretNum=7;

   int guess;

   int guessCount=0;

   int guessLimit=3;

   bool outOfGuess=false;

 

    while(secretNum !=guess && !outOfGuess){

        if(guessCount<guessLimit){

            cout<<"Enter you guess:";

            cin>>guess;

            guessCount++;

 

        }

        else{

            outOfGuess=true;

        }

 

    }

    if(outOfGuess){

        cout<<"You Loss!";

    }

    else{

         cout<<"You win!";

    }

 

    return 0;

}

22.      For Loops

#include <iostream>

 

using namespace std;

 

int main()

{

    for(int i=0; i<20; i++){

        cout<<"I love programming"<<endl;

    }

 

    return 0;

}

23.      Exponent Function

#include <iostream>

#include<cmath>

using namespace std;

 

double expo(int baseNum, int powerNum){

    int result;

    for(int i=0; i<powerNum; i++){

        result=baseNum*powerNum;

    }

    return result;

}

int main()

{

 

    cout<<expo(2,3);

 

    return 0;

}

24.      2d Arrays & Nested Loops

            #include <iostream>

 

using namespace std;

 

int main()

{

 

    int numberGrid[3][2]={

                        {40,20},

                        {55,77},

                        {21,87}

 

                        };

 

        for(int i=0; i<3; i++){

        for(int j=0; j<2; j++){

            cout<<numberGrid[i][j];

        }

        cout<<endl;

 

    }

    return 0;

}

25.      Comments

#include <iostream>

 

using namespace std;

 

int main()

{

    //Single line comment

    /*

        Multiple-lines comment

    */

 

    return 0;

}

 

26.      Pointers

            #include <iostream>

 

using namespace std;

 

int main()

{

 

    int age=20;

    int *pAge=&age;

    string name="Paul";

    string *pname=&name;

 

    cout<<*pAge<<endl;

    cout<<&*pAge<<endl;

    cout<<*pname;

 

    return 0;

}

27.      Classes & Objects

   #include <iostream>

 

using namespace std;

 

class Book{

public:

    string title;

    string author;

    int pageNo;

 

};

 

int main()

{

    Book Book1;

    Book1.title="Harry Porter";

    Book1.author="Zannanza";

    Book1.pageNo=891;

 

    cout<<" Title : "<<Book1.title<< "Author: "<<endl<<Book1.author<<" Page number :"<<Book1.pageNo<<endl;

 

    return 0;

}

28.      Constructor Functions

   #include <iostream>

 

using namespace std;

 

class Book{

public:

    string title;

    string author;

    int pageNo;

    Book(string aTitle, string aAuthor, int aPageNo){

        title=aTitle;

        author=aAuthor;

        pageNo=aPageNo;

    }

};

 

int main()

{

    Book Book1("Think and Grow Rich","Nepolian Hill",500);

    Book Book2("Think about Tomorrow","Ima",434);

 

    cout<<Book1.title <<" "<<Book1.author <<" "<<Book1.pageNo <<endl;

     cout<<Book2.title <<" "<<Book2.author <<" "<<Book2.pageNo <<endl;

 

    return 0;

}

29.      Object Functions

   #include <iostream>

 

using namespace std;

 

class Student{

public:

    string name;

    string major;

    double gpa;

 

        Student(string aName, string aMajor, double aGpa){

            name=aName;

            major=aMajor;

            gpa=aGpa;

        }

        bool hasHonor(){

            if(gpa>=3.5){

                    return true;

 

            }

            else{

                return false;

            }

 

        }

 

};

 

int main()

{

 

    Student Student1("Njohole","IT",2.3);

 

    cout<<Student1.hasHonor();

    return 0;

}

 

30.      Getters & Setters

#include <iostream>

 

using namespace std;

 

class Movie{

    private:

        string rating;

    public:

        string title;

        string director;

 

 

        Movie(string aTitle, string aDirector, string aRating){

            title=aTitle;

            director=aDirector;

            setRating(aRating);

        }

 

        void setRating(string aRating){

             if(aRating=="G" || aRating=="PG-13"|| aRating=="PG" || aRating=="R"){

                rating=aRating;

             }

            else{

                rating ="NR";

            }

        }

        string getRating(){

            return rating;

        }

};

 

int main()

{

    Movie Movie("Game Of Throne","Zannanza Njohole","PG-13");

 

    Movie.setRating("Papi");

 

    cout<<Movie.getRating();

 

    return 0;

}

 

31.      Inheritance

#include <iostream>

 

using namespace std;

 

class Chef{

    public:

        void makeChicken(){

        cout<<"The chef makes chicken"<<endl;

        }

        void makeSalad(){

           cout<<"The chef makes salad"<<endl;

        }

        void makeSpecialDish(){

            cout<<"The chef makes bbq ribs"<<endl;

        }

 

};

class ItalianChef :public Chef{

    public:

    void makePasta(){

        cout<<"The chef make Pasta"<<endl;

    }

    void makeSpecialDish(){

            cout<<"The chef makes chicken parm"<<endl;

        }

};

int main()

{

   Chef chef;

   chef.makeSpecialDish();

 

   ItalianChef italianChef;

   italianChef.makeSpecialDish();

   return 0;

}

 


Share:

0 comments:

Post a Comment