Classes and Objects

  1. Define a class student with the following specification
    Private members of class student
    admno                        integer
    sname                        20 character
    math, science       float
    total                            float
    ctotal()                        a function to calculate eng + math + science with float return type.
    Public member function of class student
    Takedata()                   Function to accept values for admno, sname, eng, science and invoke ctotal() to                                    calculate total.
    Showdata()                   Function to display all the data members on the screen.

 

Function Overloading in C++

Function Overloading is an example of polymorphism feature. It is a feature in C++ where two or more functions can have the same name but different parameters.

void test1(){ }

int test1(int a) { }

float test1(int a,float b){ }

All three functions are overloaded functions because argument(s) passed to these functions are different. Overloaded functions may or may not have different return type but it should have different parameters.

Three ways to overload the functions

There are two ways to overload the method in C++ :

  • By changing number of arguments or parameters

void sum()
{
      int a, b;
      cin>>a>>b;
      cout<<a+b;
}
void sum(int a, int b)
{
      cout<<a+b;
}
void sum(int a, int b, int c)
{
      cout<<a+b+c;
}

  • By changing the data type

void sum(int a, int b)
{
           cout<<a+b;
}
void sum(float a, float b)
{
            cout<<a+b+c;
}

  • Sequence of arguments

void Area(float l,int b)
{
cout<<“\n\tArea of Rectangle is : “<<l*b;
}
void Area(int l,float b)
{
cout<<“\n\tArea of Rectangle is : “<<l*b;
}

Virtual Functions Question

Create three classes Person, Professor and Student. The class Person should have data members name and age. The classes Professor and Student should inherit from the class Person.

The class Professor should have two integer members: publications and cur_id. There will be two member functions: getdata and putdata. The function getdata should get the input from the user: the name, age and publications of the professor. The function putdata should print the name, age, publications and the cur_id of the professor.

The class Student should have two data members: marks, which is an array of size and cur_id. It has two member functions: getdata and putdata. The function getdata should get the input from the user: the name, age, and the marks of the student in subjects. The function putdata should print the name, age, sum of the marks and the cur_id of the student.

For each object being created of the Professor or the Student class, sequential id’s should be assigned to them starting from .

Solve this problem using virtual functions, constructors and static variables. You can create more data members if you want.

Input Format

There are two types of input. If the object being created is of the Professor class, you will have to input the name, age and publications of the professor.

If the object is of the Student class, you will have to input the name, age and the marks of the student in subjects.

Constraints

1<= len<=100 , where is the length of the name.
1<=age<=80

1<=pulications<=1000

0<==marks<=100, where marks is the marks of the student in each subject.

Output Format

There are two types of output depending on the object.

If the object is of type Professor, print the space separated name, age, publications and id on a new line.

If the object is of the Student class, print the space separated name, age, the sum of the marks in subjects and id on a new line.

Sample Input

4
1
Walter 56 99
2
Jesse 18 50 48 97 76 34 98
2
Pinkman 22 10 12 0 18 45 50
1
White 58 87

Sample Output

Walter 56 99 1
Jesse 18 403 1
Pinkman 22 135 2
White 58 87 2

class Person{
public:
int age, cur_id;
char name[100];

virtual void getdata(){
};
virtual void putdata(){
};
};

class Professor: public Person
{
public:
int publications;
static int Pcur_id;
Professor()
{
cur_id = Pcur_id++;
}
void getdata()
{
cin>>name>>age>>publications;
}
void putdata()
{
cout<<name<<” “<<age<<” “<<publications<<” “<<cur_id<<endl; } }; class Student : public Person { public: int marks[6]; static int Scur_id; Student() { cur_id = Scur_id++; } void getdata() { cin>>name>>age;
for(int i=0;i<6;i++) cin>>marks[i];
}
void putdata()
{
int sum=0;
;
for(int i=0;i<6;i++)
sum+=marks[i];
cout<<name<<” “<<age<<” “<<sum<<” “<<cur_id<<endl;
}

};
int Professor :: Pcur_id=1;
int Student :: Scur_id=1;

int main(){

int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];

for(int i = 0;i < n;i++){ cin>>val;
if(val == 1){
// If val is 1 current object is of type Professor
per[i] = new Professor;

}
else per[i] = new Student; // Else the current object is of type Student

per[i]->getdata(); // Get the data from the user.

}

for(int i=0;i<n;i++) per[i]->putdata(); // Print the required output for each object.

return 0;

}

Pyramid and Pattern in C++

1) 
*
**
***
****
*****

#include<iostream>
using namespace std;

int main(){
int i, j,rows;

    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<"*";
        }
        cout<<"\n";
    }

    return 0;
}
2)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<iostream>
using namespace std;

int main(){
int i, j,rows;

    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<j;
        }
        cout<<"\n";
    }

    return 0;
}

3)
A
B B
C C C
D D D D
E E E E E

#include<iostream>
using namespace std;

int main(){
int i, j,rows;

    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<=i;j++)
        {
            cout<<char('A'+j);
        }
        cout<<"\n";
    }

    return 0;
}

4)
* * * * *
* * * *
* * * 
* *
*

#include<iostream>
using namespace std;

int main(){
int i, j,rows;

    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;i++)
    {
        for(j=i;j<=rows;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }


    return 0;

}
5)

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
/*
Key Computer Education
Koding Keys - Blog
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
*/
#include<iostream>
using namespace std;

int main(){
int i, j,rows;

    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;i++)
    {
        for(j=1;j<=rows-i+1;j++)
        {
            cout<<j;
        }
        cout<<endl;
    }
    return 0;
}


6.
1
01
010
1010
10101

#include<iostream>
using namespace std;

int main()
{
    int n,num = 1;
    cout<<"Enter number of rows: ";
    cin>>n;
    for (int i = 1 ; i<= n ; i++ )
    {
        for (int j= 1 ; j<= i ; j++ )
        {
            cout<<num;
            if ( num == 0 )
                num = 1;
            else
                num = 0;
            }
        cout<<"\n";
    }
    return 0;
}

7. Floyds Triangle – https://kodingkeys.wordpress.com/2017/05/11/7-floyds-triangle-c/

8.  

       *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
#include<iostream>
using namespace std;
int main() {
    int n;
    cout<<"Enter the number of rows in pyramid :: ";
    cin>>n;

    for(int row = 1;row<=n; row++) {
        /*spaces */
        for(int space = 1; space <= n-row; space++) {
           cout<<"  ";
        }
        /*Stars */
        for(int star =0; star != (2*row - 1);star++) {
            cout<<"* ";
        }
        cout<<"\n";
    }
    return 0;
}

or
#include<iostream>
using namespace std;

int main()
{
    int n;
    cout<<"Enter rows ";
    cin>>n;
    for(int i= 1;i<=n;i++)
    {
        for(int sp=i; sp<=n;sp++)
            cout<<" ";
        for(int j=1;j<=i;j++)
            cout<<"*";
        for(int k=1;k<i;k++)
            cout<<"*";
        cout<<endl;
    }

}

9. 

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
#include<iostream>
using namespace std;
int main() {
    int n;
    cout<<"Enter the number of rows in pyramid :: ";
    cin>>n;

    for(int row = n;row >= 1; row--) {
        /*spaces */
        for(int space = 0; space <= n-row; space++) {
           cout<<"  ";
        }
        /*Stars */
        for(int star =0; star != (2*row - 1);star++) {
            cout<<"* ";
        }
        cout<<"\n";
    }
    return 0;
}   
or


10.
#include<iostream>
using namespace std;

int main() {

	int rows, a, b, space;

	cout<<"Enter number of rows:";
	cin>>rows;
	space = rows - 1;
	for ( b = 1 ; b <= rows ; b++ ) {
		for ( a = 1 ; a <= space ; a++ )
			cout<<" ";
		space--;
		for ( a = 1 ; a <= 2*b-1 ; a++)
			cout<<"*";
		cout<<"\n";
	}

	space = 1;
	for ( b = 1 ; b <= rows - 1 ; b++ ) {
		for ( a = 1 ; a &lt<= space; a++)
			cout<<" ";
		space++;
		for ( a = 1 ; a <= 2*(rows-b)-1 ; a++ )
			cout<<"*";
		cout<<"\n";
	}
	return 0;
}


or

#include<iostream>
using namespace std;

int main()
{
    int n;
    cout<<"Enter rows ";
    cin>>n;
    for(int i= 1;i<=n;i++)
    {
        for(int sp=i; sp<n;sp++)
            cout<<" ";
        for(int j=1;j<=i;j++)
            cout<<"*";
        for(int k=1;k<i;k++)
            cout<<"*";
        cout<<endl;
    }
    for(int i= 1;i<n;i++)
    {
        for(int sp=1; sp<=i;sp++)
            cout<<" ";
        for(int j=i;j<n;j++)
            cout<<"*";
        for(int k=i+1;k<n;k++)
            cout<<"*";
        cout<<endl;
    }
}

9. Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.


#include<iostream>
using namespace std;

int main()
{
    int num, cpos=0, cneg=0, czero=0;
    char opt;
    do{
    cout<<"Enter any num";
    cin>>num;
    if(num==0)
        czero++;
    else if(num<0)
        cneg++;
    else
        cpos++;


    cout<<"Do you want to continue Y/N ";
    cin>>opt;
    }while(opt=='Y'|| opt=='y');

    cout<<"Positive = "<<cpos<<endl;
    cout<<"Negative = "<<cneg<<endl;
    cout<<"Zero     = "<<czero<<endl;


}

or

#include<iostream>
using namespace std;

int main()
{
    int num, cpos=0, cneg=0, czero=0;
    char opt;
    while(true){
    cout<<"Enter any num";
    cin>>num;
    if(num==0)
        czero++;
    else if(num<0)
        cneg++;
    else
        cpos++;


    cout<<"Do you want to continue Y/N ";
    cin>>opt;
    if(opt=='N'|| opt=='n')
        break;
    }

    cout<<"Positive = "<<cpos<<endl;
    cout<<"Negative = "<<cneg<<endl;
    cout<<"Zero     = "<<czero<<endl;


}