class and objects assignment solutions.

View previous topic View next topic Go down

class and objects assignment solutions.

Post by akarshsomani on Tue Apr 18, 2017 7:32 pm

please share class and object assignment solutions along with the question number in the comment section.

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by akarshsomani on Tue Apr 18, 2017 7:33 pm

co1:

Code:
#include<iostream>
using namespace std;
class student
{
private:
    int roll;
    char *name;
    int marks[3];
    int total()
    {
        return(marks[0]+marks[1]+marks[2]);
    }
public:
    void data(int roll, char *name, int marks[]);
    void display(void);
};

void student::data(int roll,char *name,int marks[])
{
    this->roll=roll;
    this->name=name;
    this->marks[0]=marks[0];
    this->marks[1]=marks[1];
    this->marks[2]=marks[2];
}
void student::display()
{
    int tottal=student::total();
    cout<<roll<<"\t"<<name<<"\t"<<marks[0]<<"\t"<<marks[1]<<"\t"<<marks[2]<<"\t"<<tottal;
}
main()
{
    student s;
    int marks1[]={63,99,56};
    s.data(1,"akarsh",marks1);
    s.display();
}

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by akarshsomani on Tue Apr 18, 2017 7:34 pm

co2:
Code:
#include<iostream>
using namespace std;
class store
{
private:
    int code[20];
    float price[20];
public:
    void getdata(void);
    void display(void);
    void calculate(int quantity[]);
};
void store::getdata()
{
    cout<<"enter code and price\n";
    for(int i=0;i<4;i++)
    {
        cout<<"item "<<i+1<<"\n";
        cin>>code[i];
        cin>>price[i];
    }
}
void store::display(void)
{
    cout<<"item\t"<<"code\t"<<"price\n";
    for(int i=0;i<4;i++)
    {
        cout<<"item "<<i+1<<"\t"<<code[i]<<"\t"<<price[i]<<"\n";
    }
}
void store::calculate(int quantity[])
{
    float amount=0;
for(int i=0;i<4;i++)
{
    amount+=(price[i])*(quantity[i]);
}
cout<<"total amount="<<amount;
}
main()
{
    store s;
    s.getdata();
    s.display();
    int arr[4];
    cout<<"enter amount of products item wise \n";
    for(int i=0;i<4;i++)
    {
        cout<<"item "<<i+1<<"\t";
        cin>>arr[i];
    }
    s.calculate(arr);
}

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by akarshsomani on Tue Apr 18, 2017 7:36 pm

co3:
Code:
#include<iostream>
using namespace std;
class printdata
{
public:
    void print(int i){
        cout<<i<<"\n";
    }
    void print(float i){
        cout<<i<<"\n";
    }
    void print(char *i){
        cout<<i<<"\n";
    }
};
main()
{
    printdata p;
    p.print(10);
    p.print(10.5f);
    p.print("rahul");
}


akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by akarshsomani on Tue Apr 18, 2017 7:37 pm

co5:
Code:
#include<iostream>
using namespace std;
class books
{
private:
    char title[20];
    float price;
public:
    void getdata()
    {
        cin>>title>>price;
    }
    void putdata()
    {
        cout<<title<<"\t"<<price<<"\n";
    }
};
main()
{
    books b[5];
    for(int i=0;i<5;i++)
    {
        cout<<"Input title and price\n";
        b[i].getdata();
    }
    cout<<"title"<<"\t"<<"price\n";
    for(int i=0;i<5;i++)
    {
        b[i].putdata();
    }
}

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by iamdsc_0410 on Wed Apr 19, 2017 12:20 am

co4:
#include<iostream>
#include<string.h>
using namespace std;
class person
{
private:
int age;
char name[20];
float salary;
public:
void set_data();
void set_data(int a);
void set_data(int a,char n[]);
void set_data(int a,char n[],float s);
void show_data();
};
void person::set_data()
{
cout<<"Enter the age: ";
cin>>age;
cout<<"Enter the name: ";
cin.ignore();
cin>>name;
cout<<"Enter the salary: ";
cin>>salary;
}
void person::set_data(int a)
{
age=a;
cout<<"Enter the name: ";
cin.getline(name,20);
cout<<"Enter the salary: ";
cin>>salary;
}
void person::set_data(int a,char n[])
{
age=a;
strcpy(name,n);
cout<<"Enter the salary: ";
cin>>salary;
}
void person::set_data(int a,char n[],float s)
{
age=a;
strcpy(name,n);
salary=s;
}
void person::show_data()
{
cout<<"NAME: "<<name;
cout<<"\nAGE: "<<age;
cout<<"\nSALARY: "<<salary<<endl;
}
int main()
{
person p;
p.set_data(25);
p.show_data();
return 0;
}
avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 20
Location : IIIT Kalyani

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by akarshsomani on Wed Apr 19, 2017 12:45 pm

co11:
Code:
#include<iostream>
using namespace std;
class number
{
private:
    int a;int b;
public:
    void get()
    {
        a=10;b=20;
    }

    int compare(number *n)
    {

        if(n->a>n->b)
            return n->a;
        else
            return n->b;
    }
};
main()
{
    void (number::*fp)(void);
    fp=&number::get;
    number *n=new number;
    (n->*fp)();
    cout<<n->compare(n);
  }


Last edited by akarshsomani on Wed Apr 19, 2017 4:54 pm; edited 1 time in total

akarshsomani

Posts : 21
Join date : 2017-04-04

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by ASP.net on Wed Apr 19, 2017 4:42 pm

CO-10, In this u can define a constructor also to initialize the members.
Code:
#include<iostream>
using namespace std;
class X;
class Y;
int sum(X,Y);
class Y
{
    int c=10;
public:
    int sum(X &,Y &);
};
class X
{
    int x=9;
public:
    friend int Y::sum(X &,Y &);
};
int Y::sum(X &obj1, Y &obj2)
{
    return obj1.x+obj2.c;
}
int main()
{
   X x1;
   Y y1;
   cout<<y1.sum(x1,y1);
}

ASP.net

Posts : 1
Join date : 2017-04-14

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by iamdsc_0410 on Wed Apr 19, 2017 11:23 pm

CO8.
Code:
//passing objects as arguments
#include<iostream>
#include<stdlib.h>
using namespace std;
class fraction
{
private:
int num;
int deno;
void simplify();
int GCD(int,int);
public:
void get_data()
{
cout<<"Enter the numerator :";
cin>>num;
cout<<"Enter the denominator :";
cin>>deno;
if(deno==0)
{
cout<<"Fraction Not Possible";
exit(1);
}
}
void display_data()
{
simplify();
cout<<num<<"/"<<deno;
}
void add(fraction f1,fraction f2);
void subtract(fraction &f1,fraction &f2);
void multiply(fraction *f1,fraction *f2);
};
void fraction::simplify()
{
int common_divisor=GCD(num,deno);
num/=common_divisor;
deno/=common_divisor;
}
int fraction::GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
void fraction::add(fraction f1,fraction f2)
{
num=(f1.num*f2.deno)+(f2.num*f1.deno);
deno=f1.deno*f2.deno;
display_data();
}
void fraction::subtract(fraction &f1,fraction &f2)
{
num=(f1.num*f2.deno)-(f2.num*f1.deno);
deno=f1.deno*f2.deno;
display_data();
}
void fraction::multiply(fraction *f1,fraction *f2)
{
num=f1->num*f2->num;
deno=f1->deno*f2->deno;
display_data();
}
int main()
{
fraction f1,f2,f3;
f1.get_data();
f2.get_data();
cout<<"Result of addition = ";
f3.add(f1,f2);
cout<<"\nResult of subtraction = ";
f3.subtract(f1,f2);
cout<<"\nResult of multiplication = ";
f3.multiply(&f1,&f2);
return 0;
}
avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 20
Location : IIIT Kalyani

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by iamdsc_0410 on Fri Apr 21, 2017 8:34 pm

CO7;
Code:
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
int roll;
char name[20];
int marks[5];
int total(void);
public:
void getdata(int r,char *n,int m[])
{
int j;
roll=r;
strcpy(name,n);
for(j=0;j<5;j++)
marks[j]=m[j];
}
void disp()
{
cout<<"Roll :"<<roll<<endl;
cout<<"Name :"<<name<<"\n";
cout<<"Total Marks :"<<total();
cout<<endl;
}
};
int student::total(void)
{
int k;
int sum=0;
for(k=0;k<5;k++)
{
sum+=marks[k];
}
return sum;
}
int main()
{

int a,r,m[5],j,i;
char n[20];
cout<<"Enter the number of students\n";
cin>>a;
student s[a];
for(i=0;i<a;i++)
{
cin>>r;
cin>>n;
for(j=0;j<5;j++)
{
cin>>m[j];
}
s[i].getdata(r,n,m);
}
for(i=0;i<a;i++)
{
s[i].disp();
}
return 0;
}
avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 20
Location : IIIT Kalyani

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by iamdsc_0410 on Fri Apr 21, 2017 9:01 pm

CO 9:
Code:
#include<iostream>
#include<string.h>
using namespace std;
class Movie
{
private:
char *name;
char *actor;
char *actress;
public:
void set_data(char *n,char *a,char *b);
char *show_name() const;//constant member functions
char *show_actor() const;
char *show_actress() const;
};
void Movie::set_data(char *n,char *a,char *b)
{
name=new char[strlen(n)+1];
strcpy(name,n);
actor=new char[strlen(a)+1];
strcpy(actor,a);
actress=new char[strlen(b)+1];
strcpy(actress,b);
}
char * Movie::show_name() const
{
return name;
}
char * Movie::show_actor() const
{
return actor;
}
char * Movie::show_actress() const
{
return actress;
}
void display(Movie const &m)//constant object implicitly passed
{
cout<<"\nMovie : "<<m.show_name();
cout<<"\nActor :"<<m.show_actor();
cout<<"\nActress :"<<m.show_actress();
}
int main()
{
Movie m1,m2;
m1.set_data("Three Idiots","Aamir Khan","Kareena Kapoor");
cout<<"\nMovie :"<<m1.show_name();
cout<<"\nActor :"<<m1.show_actor();
cout<<"\nActress :"<<m1.show_actress();
m2.set_data("Namastey London","Akshay Kumar","Katrina Kaif");
display(m2);
return 0;
}

avatar
iamdsc_0410

Posts : 14
Join date : 2017-04-11
Age : 20
Location : IIIT Kalyani

View user profile

Back to top Go down

Re: class and objects assignment solutions.

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum